diff --git a/lib/checknonreentrantfunctions.h b/lib/checknonreentrantfunctions.h index 60326353b..890c72a28 100644 --- a/lib/checknonreentrantfunctions.h +++ b/lib/checknonreentrantfunctions.h @@ -74,11 +74,11 @@ private: // generate messages for (unsigned int i = 0; i < (sizeof(non_reentrant_functions_list) / sizeof(char *)); ++i) { - std::string strMsg("Found non reentrant function \'"); + std::string strMsg("Non reentrant function '"); strMsg+=non_reentrant_functions_list[i]; - strMsg+= "\'. For threadsafe applications it is recommended to use the reentrant replacement function \'"; + strMsg+= "' called. For threadsafe applications it is recommended to use the reentrant replacement function '"; strMsg+=non_reentrant_functions_list[i]; - strMsg+="_r\'"; + strMsg+="_r'."; _nonReentrantFunctions[non_reentrant_functions_list[i]] = strMsg; } } diff --git a/lib/checkobsoletefunctions.h b/lib/checkobsoletefunctions.h index 7fe5c02fe..3985a7de0 100644 --- a/lib/checkobsoletefunctions.h +++ b/lib/checkobsoletefunctions.h @@ -64,67 +64,61 @@ private: /** init obsolete functions list ' */ void initObsoleteFunctions() { - _obsoletePosixFunctions["bsd_signal"] = "Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function"; + // Obsolete posix functions, which messages suggest only one alternative and doesn't contain additional information. + const struct { + const char* bad; + const char* good; + } posix_stdmsgs[] = { + {"bsd_signal", "sigaction"}, + {"gethostbyaddr", "getnameinfo"}, + {"gethostbyname", "getaddrinfo"}, + {"bcmp", "memcmp"}, + {"bzero", "memset"}, + {"ecvt", "sprintf"}, + {"fcvt", "sprintf"}, + {"gcvt", "sprintf"}, + {"getwd", "getcwd"}, + {"index", "strchr"}, // See #2334 (using the Qt Model/View function 'index') + {"rindex", "strrchr"}, + {"pthread_attr_getstackaddr", "pthread_attr_getstack"}, + {"pthread_attr_setstackaddr", "pthread_attr_setstack"}, + {"vfork", "fork"}, + {"wcswcs", "wcsstr"}, + {"rand_r", "rand"}, + {"utime", "utimensat"}, + {"asctime", "strftime"}, + {"asctime_r", "strftime"}, + {"ctime", "strftime"}, + {"ctime_r", "strftime"} + }; - _obsoletePosixFunctions["gethostbyaddr"] = "Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getnameinfo' function"; - _obsoletePosixFunctions["gethostbyname"] = "Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getaddrinfo' function"; + for (std::size_t i = 0; i < (sizeof(posix_stdmsgs) / sizeof(*posix_stdmsgs)); ++i) { + _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."; + } - _obsoletePosixFunctions["usleep"] = "Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function\n" - "Found obsolete function 'usleep'. 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."; + _obsoletePosixFunctions["usleep"] = "Obsolete function 'usleep' called. It is recommended to use the 'nanosleep' or 'setitimer' function instead.\n" + "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."; - _obsoletePosixFunctions["bcmp"]="Found obsolete function 'bcmp'. It is recommended that new applications use the 'memcmp' function"; - _obsoletePosixFunctions["bcopy"]="Found obsolete function 'bcopy'. It is recommended that new applications use the 'memmove' or 'memcpy' functions"; - _obsoletePosixFunctions["bzero"]="Found obsolete function 'bzero'. It is recommended that new applications use the 'memset' function"; + _obsoletePosixFunctions["bcopy"] = "Obsolete function 'bcopy' called. It is recommended to use the 'memmove' or 'memcpy' function instead."; - _obsoletePosixFunctions["ecvt"]="Found obsolete function 'ecvt'. It is recommended that new applications use the 'sprintf' function"; - _obsoletePosixFunctions["fcvt"]="Found obsolete function 'fcvt'. It is recommended that new applications use the 'sprintf' function"; - _obsoletePosixFunctions["gcvt"]="Found obsolete function 'gcvt'. It is recommended that new applications use the 'sprintf' function"; + _obsoletePosixFunctions["ftime"] = "Obsolete function 'ftime' called. It is recommended to use time(), gettimeofday() or clock_gettime() instead."; - _obsoletePosixFunctions["ftime"]="Found obsolete function 'ftime'.\n" - "It is recommended that new applications use time(), gettimeofday(), or clock_gettime() instead. " - "For high-resolution timing on Windows, QueryPerformanceCounter() and QueryPerformanceFrequency may be used."; + _obsoletePosixFunctions["getcontext"] = "Obsolete function 'getcontext' called. Due to portability issues, applications are recommended to be rewritten to use POSIX threads."; + _obsoletePosixFunctions["makecontext"] = "Obsolete function 'makecontext' called. Due to portability issues, applications are recommended to be rewritten to use POSIX threads."; + _obsoletePosixFunctions["swapcontext"] = "Obsolete function 'swapcontext' called. Due to portability issues, applications are recommended to be rewritten to use POSIX threads."; - _obsoletePosixFunctions["getcontext"] = "Found obsolete function 'getcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads"; - _obsoletePosixFunctions["makecontext"] = "Found obsolete function 'makecontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads"; - _obsoletePosixFunctions["swapcontext"] = "Found obsolete function 'swapcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads"; + _obsoletePosixFunctions["scalbln"] = "Obsolete function 'scalb' called. It is recommended to use 'scalbln', 'scalblnf' or 'scalblnl' instead."; - _obsoletePosixFunctions["getwd"] = "Found obsolete function 'getwd'. It is recommended that new applications use the 'getcwd' function"; + _obsoletePosixFunctions["ualarm"] = "Obsolete function 'ualarm' called. It is recommended to use 'timer_create', 'timer_delete', 'timer_getoverrun', 'timer_gettime' or 'timer_settime' instead."; - // See #2334 (using the Qt Model/View function 'index') - _obsoletePosixFunctions["index"] ="Found obsolete function 'index'. It is recommended to use the function 'strchr' instead"; + _obsoletePosixFunctions["tmpnam"] = "Obsolete function 'tmpnam' called. It is recommended to use 'tmpfile', 'mkstemp' or 'mkdtemp' instead."; - _obsoletePosixFunctions["rindex"] = "Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead"; + _obsoletePosixFunctions["tmpnam_r"] = "Obsolete function 'tmpnam_r' called. It is recommended to use 'tmpfile', 'mkstemp' or 'mkdtemp' instead."; - _obsoletePosixFunctions["pthread_attr_getstackaddr"] = "Found obsolete function 'pthread_attr_getstackaddr'. It is recommended that new applications use the 'pthread_attr_getstack' function"; - _obsoletePosixFunctions["pthread_attr_setstackaddr"] = "Found obsolete function 'pthread_attr_setstackaddr'. It is recommended that new applications use the 'pthread_attr_setstack' function"; - - _obsoletePosixFunctions["scalbln"] = "Found obsolete function 'scalb'. It is recommended to use either 'scalbln', 'scalblnf' or 'scalblnl' instead of this function"; - - _obsoletePosixFunctions["ualarm"] = "Found obsolete function 'ualarm'. It is recommended to use either 'timer_create', 'timer_delete', 'timer_getoverrun', 'timer_gettime', or 'timer_settime' instead of this function"; - - _obsoletePosixFunctions["vfork"] = "Found obsolete function 'vfork'. It is recommended to use the function 'fork' instead"; - - _obsoletePosixFunctions["wcswcs"] = "Found obsolete function 'wcswcs'. It is recommended to use the function 'wcsstr' instead"; - - _obsoletePosixFunctions["rand_r"] = "Found obsolete function 'rand_r'. It is recommended to use the function 'rand' instead"; - - _obsoletePosixFunctions["tmpnam"] = "Found obsolete function 'tmpnam'. It is recommended to use either 'tmpfile', 'mkstemp', or 'mkdtemp' instead for this function"; - - _obsoletePosixFunctions["tmpnam_r"] = "Found obsolete function 'tmpnam_r'. It is recommended to use either 'tmpfile', 'mkstemp', or 'mkdtemp' instead for this function"; - - _obsoletePosixFunctions["utime"] = "Found obsolete function 'utime'. It is recommended to use the function 'utimensat' instead"; - - _obsoletePosixFunctions["asctime"] = "Found obsolete function 'asctime'. It is recommended to use the function 'strftime' instead"; - - _obsoletePosixFunctions["asctime_r"] = "Found obsolete function 'asctime_r'. It is recommended to use the function 'strftime' instead"; - - _obsoletePosixFunctions["ctime"] = "Found obsolete function 'ctime'. It is recommended to use the function 'strftime' instead"; - - _obsoletePosixFunctions["ctime_r"] = "Found obsolete function 'ctime'. It is recommended to use the function 'strftime' instead"; - - _obsoleteStandardFunctions["gets"] = "Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n" - "Found obsolete function 'gets'. With gets you'll get buffer overruns if the input data too big for the buffer. It is recommended to use the function 'fgets' instead."; - _obsoleteC99Functions["alloca"] = "Found obsolete function 'alloca'. It is recommended to use a variable length array.\nFound obsolete function 'alloca'. It is recommended to use a variable length array or a dynamically allocated array. 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)."; + _obsoleteStandardFunctions["gets"] = "Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n" + "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."; + _obsoleteC99Functions["alloca"] = "Obsolete function 'alloca' called. It is recommended to use a variable length array.\n" + "The obsolete function 'alloca' is called. 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)."; } diff --git a/test/testnonreentrantfunctions.cpp b/test/testnonreentrantfunctions.cpp index 64fe77968..7afbeb2a2 100644 --- a/test/testnonreentrantfunctions.cpp +++ b/test/testnonreentrantfunctions.cpp @@ -62,7 +62,7 @@ private: " char *cpwd;" " crypt(pwd, cpwd);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (portability) Found non reentrant function 'crypt'. For threadsafe applications it is recommended to use the reentrant replacement function 'crypt_r'\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (portability) Non reentrant function 'crypt' called. For threadsafe applications it is recommended to use the reentrant replacement function 'crypt_r'.\n", errout.str()); check("void f()\n" "{\n" @@ -70,7 +70,7 @@ private: " char *cpwd;" " crypt(pwd, cpwd);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (portability) Found non reentrant function 'crypt'. For threadsafe applications it is recommended to use the reentrant replacement function 'crypt_r'\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (portability) Non reentrant function 'crypt' called. For threadsafe applications it is recommended to use the reentrant replacement function 'crypt_r'.\n", errout.str()); check("int f()\n" "{\n" @@ -86,14 +86,14 @@ private: " time_t t = 0;" " std::localtime(&t);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (portability) Found non reentrant function 'localtime'. For threadsafe applications it is recommended to use the reentrant replacement function 'localtime_r'\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (portability) Non reentrant function 'localtime' called. For threadsafe applications it is recommended to use the reentrant replacement function 'localtime_r'.\n", errout.str()); // Passed as function argument check("int f()\n" "{\n" " printf(\"Magic guess: %d\n\", getpwent());\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (portability) Found non reentrant function 'getpwent'. For threadsafe applications it is recommended to use the reentrant replacement function 'getpwent_r'\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (portability) Non reentrant function 'getpwent' called. For threadsafe applications it is recommended to use the reentrant replacement function 'getpwent_r'.\n", errout.str()); // Pass return value check("int f()\n" @@ -101,14 +101,14 @@ private: " time_t t = 0;" " struct tm *foo = localtime(&t);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (portability) Found non reentrant function 'localtime'. For threadsafe applications it is recommended to use the reentrant replacement function 'localtime_r'\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (portability) Non reentrant function 'localtime' called. For threadsafe applications it is recommended to use the reentrant replacement function 'localtime_r'.\n", errout.str()); // Access via global namespace check("int f()\n" "{\n" " ::getpwent();\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (portability) Found non reentrant function 'getpwent'. For threadsafe applications it is recommended to use the reentrant replacement function 'getpwent_r'\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (portability) Non reentrant function 'getpwent' called. For threadsafe applications it is recommended to use the reentrant replacement function 'getpwent_r'.\n", errout.str()); // Be quiet on function definitions check("int getpwent()\n" diff --git a/test/testobsoletefunctions.cpp b/test/testobsoletefunctions.cpp index c98eeb29e..7a6b1a7cf 100644 --- a/test/testobsoletefunctions.cpp +++ b/test/testobsoletefunctions.cpp @@ -93,7 +93,7 @@ private: "{\n" " bsd_signal(SIGABRT, SIG_IGN);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (style) Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'bsd_signal' called. It is recommended to use the function 'sigaction' instead.\n", errout.str()); check("int f()\n" "{\n" @@ -112,7 +112,7 @@ private: " exit(1);\n" " }\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (style) Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getaddrinfo' function\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (style) Obsolete function 'gethostbyname' called. It is recommended to use the function 'getaddrinfo' instead.\n", errout.str()); } void testgethostbyaddr() { @@ -124,7 +124,7 @@ private: " exit(1);\n" " }\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5]: (style) Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getnameinfo' function\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (style) Obsolete function 'gethostbyaddr' called. It is recommended to use the function 'getnameinfo' instead.\n", errout.str()); } void testusleep() { @@ -132,7 +132,7 @@ private: "{\n" " usleep( 1000 );\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (style) Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'usleep' called. It is recommended to use the 'nanosleep' or 'setitimer' function instead.\n", errout.str()); } void testindex() { @@ -172,7 +172,7 @@ private: " const char i = index(var, 0);\n" " return i;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (style) Found obsolete function 'index'. It is recommended to use the function 'strchr' instead\n", + ASSERT_EQUALS("[test.cpp:4]: (style) Obsolete function 'index' called. It is recommended to use the function 'strchr' instead.\n", errout.str()); } @@ -180,7 +180,7 @@ private: check("void TDataModel::forceRowRefresh(int row) {\n" " emit dataChanged(index(row, 0), index(row, columnCount() - 1));\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2]: (style) Found obsolete function 'index'. It is recommended to use the function 'strchr' instead\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (style) Obsolete function 'index' called. It is recommended to use the function 'strchr' instead.\n", errout.str()); } void testrindex() { @@ -195,7 +195,7 @@ private: " const char var[7] = 'rindex';\n" " print(rindex(var, 0));\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (style) Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (style) Obsolete function 'rindex' called. It is recommended to use the function 'strrchr' instead.\n", errout.str()); } @@ -213,7 +213,7 @@ private: "{\n" " char *x = gets();\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (style) Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n", errout.str()); } void testalloca() { @@ -221,7 +221,7 @@ private: "{\n" " char *x = alloca(10);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (style) Found obsolete function 'alloca'. It is recommended to use a variable length array.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'alloca' called. It is recommended to use a variable length array.\n", errout.str()); } // ticket #3121 @@ -244,7 +244,7 @@ private: "{\n" " char *x = std::gets(str);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (style) Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n", errout.str()); } // multiple use @@ -254,7 +254,8 @@ private: " char *x = std::gets(str);\n" " usleep( 1000 );\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (style) Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n[test.cpp:4]: (style) Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n" + "[test.cpp:4]: (style) Obsolete function 'usleep' called. It is recommended to use the 'nanosleep' or 'setitimer' function instead.\n", errout.str()); } void test_c_declaration() { @@ -264,14 +265,14 @@ private: " char s [ 10 ] ;\n" " gets ( s ) ;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5]: (style) Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (style) Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n", errout.str()); check("int getcontext(ucontext_t *ucp);\n" "int f (ucontext_t *ucp)\n" "{\n" " getcontext ( ucp ) ;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (style) Found obsolete function 'getcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (style) Obsolete function 'getcontext' called. Due to portability issues, applications are recommended to be rewritten to use POSIX threads.\n", errout.str()); } void test_function_with_body() {