Cppcheck
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
checkobsolescentfunctions.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2015 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 
20 //---------------------------------------------------------------------------
21 #ifndef checkobsoletefunctionsH
22 #define checkobsoletefunctionsH
23 //---------------------------------------------------------------------------
24 
25 #include "config.h"
26 #include "check.h"
27 #include <string>
28 #include <map>
29 
30 
31 /// @addtogroup Checks
32 /// @{
33 
34 /**
35  * @brief Using obsolete functions that are always insecure to use.
36  */
37 
39 public:
40  /** This constructor is used when registering the CheckObsoleteFunctions */
41  CheckObsoleteFunctions() : Check(myName()) {
42  initObsoleteFunctions();
43  }
44 
45  /** This constructor is used when running checks. */
46  CheckObsoleteFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
47  : Check(myName(), tokenizer, settings, errorLogger) {
48  }
49 
50  void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
51  CheckObsoleteFunctions checkObsoleteFunctions(tokenizer, settings, errorLogger);
52  checkObsoleteFunctions.obsoleteFunctions();
53  }
54 
55  /** Check for obsolete functions */
56  void obsoleteFunctions();
57 
58 private:
59  /* function name / error message */
60  static std::map<std::string, std::string> _obsoleteStandardFunctions;
61  static std::map<std::string, std::string> _obsoletePosixFunctions;
62  static std::map<std::string, std::string> _obsoleteC99Functions;
63 
64  /** init obsolete functions list ' */
65  static void initObsoleteFunctions() {
66  // Obsolete posix functions, which messages suggest only one alternative and doesn't contain additional information.
67  const struct {
68  const char* bad;
69  const char* good;
70  } posix_stdmsgs[] = {
71  {"bsd_signal", "sigaction"},
72  {"gethostbyaddr", "getnameinfo"},
73  {"gethostbyname", "getaddrinfo"},
74  {"bcmp", "memcmp"},
75  {"bzero", "memset"},
76  {"ecvt", "sprintf"},
77  {"fcvt", "sprintf"},
78  {"gcvt", "sprintf"},
79  {"getwd", "getcwd"},
80  {"index", "strchr"}, // See #2334 (using the Qt Model/View function 'index')
81  {"rindex", "strrchr"},
82  {"pthread_attr_getstackaddr", "pthread_attr_getstack"},
83  {"pthread_attr_setstackaddr", "pthread_attr_setstack"},
84  {"vfork", "fork"},
85  {"wcswcs", "wcsstr"},
86  {"rand_r", "rand"},
87  {"utime", "utimensat"},
88  {"asctime_r", "strftime"},
89  {"ctime_r", "strftime"}
90  };
91 
92  for (std::size_t i = 0; i < (sizeof(posix_stdmsgs) / sizeof(*posix_stdmsgs)); ++i) {
93  _obsoletePosixFunctions[posix_stdmsgs[i].bad] = "Obsolete function '" + std::string(posix_stdmsgs[i].bad) + "' called. It is recommended to use the function '" + posix_stdmsgs[i].good + "' instead.";
94  }
95 
96  _obsoletePosixFunctions["usleep"] = "Obsolete function 'usleep' called. It is recommended to use the 'nanosleep' or 'setitimer' function instead.\n"
97  "The obsolete function 'usleep' is called. POSIX.1-2001 declares usleep() function obsolete and POSIX.1-2008 removes it. It is recommended that new applications use the 'nanosleep' or 'setitimer' function.";
98 
99  _obsoletePosixFunctions["bcopy"] = "Obsolete function 'bcopy' called. It is recommended to use the 'memmove' or 'memcpy' function instead.";
100 
101  _obsoletePosixFunctions["ftime"] = "Obsolete function 'ftime' called. It is recommended to use time(), gettimeofday() or clock_gettime() instead.";
102 
103  _obsoletePosixFunctions["getcontext"] = "Obsolete function 'getcontext' called. Due to portability issues, applications are recommended to be rewritten to use POSIX threads.";
104  _obsoletePosixFunctions["makecontext"] = "Obsolete function 'makecontext' called. Due to portability issues, applications are recommended to be rewritten to use POSIX threads.";
105  _obsoletePosixFunctions["swapcontext"] = "Obsolete function 'swapcontext' called. Due to portability issues, applications are recommended to be rewritten to use POSIX threads.";
106 
107  _obsoletePosixFunctions["scalbln"] = "Obsolete function 'scalb' called. It is recommended to use 'scalbln', 'scalblnf' or 'scalblnl' instead.";
108 
109  _obsoletePosixFunctions["ualarm"] = "Obsolete function 'ualarm' called. It is recommended to use 'timer_create', 'timer_delete', 'timer_getoverrun', 'timer_gettime' or 'timer_settime' instead.";
110 
111  _obsoletePosixFunctions["tmpnam"] = "Obsolete function 'tmpnam' called. It is recommended to use 'tmpfile', 'mkstemp' or 'mkdtemp' instead.";
112 
113  _obsoletePosixFunctions["tmpnam_r"] = "Obsolete function 'tmpnam_r' called. It is recommended to use 'tmpfile', 'mkstemp' or 'mkdtemp' instead.";
114 
115  _obsoleteStandardFunctions["gets"] = "Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n"
116  "The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun if the input data exceeds the size of the buffer. It is recommended to use the function 'fgets' instead.";
117  _obsoleteC99Functions["alloca"] = "Obsolete function 'alloca' called. In C99 and later it is recommended to use a variable length array instead.\n"
118  "The obsolete function 'alloca' is called. In C99 and later it is recommended to use a variable length array or a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons (http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca).";
119  _obsoleteC99Functions["asctime"] = "Obsolete function 'asctime' called. It is recommended to use the function 'strftime' instead.";
120  // ctime is obsolete - it's not threadsafe. but there is no good replacement.
121  //_obsoleteC99Functions["ctime"] = "Obsolete function 'ctime' called. It is recommended to use the function 'strftime' instead.";
122  }
123 
124  void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
125  CheckObsoleteFunctions c(0, settings, errorLogger);
126 
127  for (std::map<std::string, std::string>::const_iterator it = _obsoleteStandardFunctions.begin(); it != _obsoleteStandardFunctions.end(); ++it)
128  c.reportError(0, Severity::style, "obsoleteFunctions" + it->first, it->second);
129  for (std::map<std::string, std::string>::const_iterator it = _obsoleteC99Functions.begin(); it != _obsoleteC99Functions.end(); ++it)
130  c.reportError(0, Severity::style, "obsoleteFunctions" + it->first, it->second);
131  for (std::map<std::string, std::string>::const_iterator it = _obsoletePosixFunctions.begin(); it != _obsoletePosixFunctions.end(); ++it)
132  c.reportError(0, Severity::style, "obsoleteFunctions" + it->first, it->second);
133  }
134 
135  static std::string myName() {
136  return "Obsolete functions";
137  }
138 
139  std::string classInfo() const {
140  std::string info = "Warn if any of these obsolete functions are used:\n";
141  for (std::map<std::string, std::string>::const_iterator it = _obsoleteStandardFunctions.begin(); it != _obsoleteStandardFunctions.end(); ++it)
142  info += "- " + it->first + "\n";
143  for (std::map<std::string, std::string>::const_iterator it = _obsoleteC99Functions.begin(); it != _obsoleteC99Functions.end(); ++it)
144  info += "- " + it->first + "\n";
145  for (std::map<std::string, std::string>::const_iterator it = _obsoletePosixFunctions.begin(); it != _obsoletePosixFunctions.end(); ++it)
146  info += "- " + it->first + "\n";
147  return info;
148  }
149 };
150 /// @}
151 //---------------------------------------------------------------------------
152 #endif // checkobsoletefunctionsH