Message refactorization: checkobsoletefunctions.h and checknonreentrantfunctions.h

This commit is contained in:
PKEuS 2012-07-29 02:30:03 -07:00
parent 9d0fb1a169
commit 2f95b97c66
4 changed files with 68 additions and 73 deletions

View File

@ -74,11 +74,11 @@ private:
// generate messages // generate messages
for (unsigned int i = 0; i < (sizeof(non_reentrant_functions_list) / sizeof(char *)); ++i) { 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+=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+=non_reentrant_functions_list[i];
strMsg+="_r\'"; strMsg+="_r'.";
_nonReentrantFunctions[non_reentrant_functions_list[i]] = strMsg; _nonReentrantFunctions[non_reentrant_functions_list[i]] = strMsg;
} }
} }

View File

@ -64,67 +64,61 @@ private:
/** init obsolete functions list ' */ /** init obsolete functions list ' */
void initObsoleteFunctions() { 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"; for (std::size_t i = 0; i < (sizeof(posix_stdmsgs) / sizeof(*posix_stdmsgs)); ++i) {
_obsoletePosixFunctions["gethostbyname"] = "Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getaddrinfo' function"; _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" _obsoletePosixFunctions["usleep"] = "Obsolete function 'usleep' called. It is recommended to use the 'nanosleep' or 'setitimer' function instead.\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."; "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"] = "Obsolete function 'bcopy' called. It is recommended to use the 'memmove' or 'memcpy' function instead.";
_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["ecvt"]="Found obsolete function 'ecvt'. 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["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"]="Found obsolete function 'ftime'.\n" _obsoletePosixFunctions["getcontext"] = "Obsolete function 'getcontext' called. Due to portability issues, applications are recommended to be rewritten to use POSIX threads.";
"It is recommended that new applications use time(), gettimeofday(), or clock_gettime() instead. " _obsoletePosixFunctions["makecontext"] = "Obsolete function 'makecontext' called. Due to portability issues, applications are recommended to be rewritten to use POSIX threads.";
"For high-resolution timing on Windows, QueryPerformanceCounter() and QueryPerformanceFrequency may be used."; _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["scalbln"] = "Obsolete function 'scalb' called. It is recommended to use 'scalbln', 'scalblnf' or 'scalblnl' instead.";
_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["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["tmpnam"] = "Obsolete function 'tmpnam' called. It is recommended to use 'tmpfile', 'mkstemp' or 'mkdtemp' instead.";
_obsoletePosixFunctions["index"] ="Found obsolete function 'index'. It is recommended to use the function 'strchr' 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"; _obsoleteStandardFunctions["gets"] = "Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n"
_obsoletePosixFunctions["pthread_attr_setstackaddr"] = "Found obsolete function 'pthread_attr_setstackaddr'. It is recommended that new applications use the 'pthread_attr_setstack' function"; "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"
_obsoletePosixFunctions["scalbln"] = "Found obsolete function 'scalb'. It is recommended to use either 'scalbln', 'scalblnf' or 'scalblnl' instead of this function"; "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).";
_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).";
} }

View File

@ -62,7 +62,7 @@ private:
" char *cpwd;" " char *cpwd;"
" crypt(pwd, cpwd);\n" " crypt(pwd, cpwd);\n"
"}\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" check("void f()\n"
"{\n" "{\n"
@ -70,7 +70,7 @@ private:
" char *cpwd;" " char *cpwd;"
" crypt(pwd, cpwd);\n" " crypt(pwd, cpwd);\n"
"}\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" check("int f()\n"
"{\n" "{\n"
@ -86,14 +86,14 @@ private:
" time_t t = 0;" " time_t t = 0;"
" std::localtime(&t);\n" " std::localtime(&t);\n"
"}\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 // Passed as function argument
check("int f()\n" check("int f()\n"
"{\n" "{\n"
" printf(\"Magic guess: %d\n\", getpwent());\n" " printf(\"Magic guess: %d\n\", getpwent());\n"
"}\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 // Pass return value
check("int f()\n" check("int f()\n"
@ -101,14 +101,14 @@ private:
" time_t t = 0;" " time_t t = 0;"
" struct tm *foo = localtime(&t);\n" " struct tm *foo = localtime(&t);\n"
"}\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 // Access via global namespace
check("int f()\n" check("int f()\n"
"{\n" "{\n"
" ::getpwent();\n" " ::getpwent();\n"
"}\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 // Be quiet on function definitions
check("int getpwent()\n" check("int getpwent()\n"

View File

@ -93,7 +93,7 @@ private:
"{\n" "{\n"
" bsd_signal(SIGABRT, SIG_IGN);\n" " bsd_signal(SIGABRT, SIG_IGN);\n"
"}\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" check("int f()\n"
"{\n" "{\n"
@ -112,7 +112,7 @@ private:
" exit(1);\n" " exit(1);\n"
" }\n" " }\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() { void testgethostbyaddr() {
@ -124,7 +124,7 @@ private:
" exit(1);\n" " exit(1);\n"
" }\n" " }\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() { void testusleep() {
@ -132,7 +132,7 @@ private:
"{\n" "{\n"
" usleep( 1000 );\n" " usleep( 1000 );\n"
"}\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() { void testindex() {
@ -172,7 +172,7 @@ private:
" const char i = index(var, 0);\n" " const char i = index(var, 0);\n"
" return i;\n" " return i;\n"
"}\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()); errout.str());
} }
@ -180,7 +180,7 @@ private:
check("void TDataModel::forceRowRefresh(int row) {\n" check("void TDataModel::forceRowRefresh(int row) {\n"
" emit dataChanged(index(row, 0), index(row, columnCount() - 1));\n" " emit dataChanged(index(row, 0), index(row, columnCount() - 1));\n"
"}\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() { void testrindex() {
@ -195,7 +195,7 @@ private:
" const char var[7] = 'rindex';\n" " const char var[7] = 'rindex';\n"
" print(rindex(var, 0));\n" " print(rindex(var, 0));\n"
"}\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" "{\n"
" char *x = gets();\n" " char *x = gets();\n"
"}\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() { void testalloca() {
@ -221,7 +221,7 @@ private:
"{\n" "{\n"
" char *x = alloca(10);\n" " char *x = alloca(10);\n"
"}\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 // ticket #3121
@ -244,7 +244,7 @@ private:
"{\n" "{\n"
" char *x = std::gets(str);\n" " char *x = std::gets(str);\n"
"}\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 // multiple use
@ -254,7 +254,8 @@ private:
" char *x = std::gets(str);\n" " char *x = std::gets(str);\n"
" usleep( 1000 );\n" " usleep( 1000 );\n"
"}\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() { void test_c_declaration() {
@ -264,14 +265,14 @@ private:
" char s [ 10 ] ;\n" " char s [ 10 ] ;\n"
" gets ( s ) ;\n" " gets ( s ) ;\n"
"}\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" check("int getcontext(ucontext_t *ucp);\n"
"int f (ucontext_t *ucp)\n" "int f (ucontext_t *ucp)\n"
"{\n" "{\n"
" getcontext ( ucp ) ;\n" " getcontext ( ucp ) ;\n"
"}\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() { void test_function_with_body() {