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"
" int bsd_signal(0);\n"
" return bsd_signal;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
voidtestgethostbyname() {
check("void f()\n"
"{\n"
" struct hostent *hp;\n"
" if(!hp = gethostbyname('127.0.0.1')) {\n"
" exit(1);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (style) Obsolete function 'gethostbyname' called. It is recommended to use the function 'getaddrinfo' instead.\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());
}
voidtestusleep() {
check("void f()\n"
"{\n"
" usleep( 1000 );\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'usleep' called. It is recommended to use the 'nanosleep' or 'setitimer' function instead.\n", errout.str());
}
voidtestindex() {
check("namespace n1 {\n"
" int index(){};\n"
"}\n"
"int main()\n"
"{\n"
" n1::index();\n"
"}");
ASSERT_EQUALS("", errout.str());
check("std::size_t f()\n"
"{\n"
" std::size_t index(0);\n"
" index++;\n"
" return index;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int f()\n"
"{\n"
" return this->index();\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"
" int index( 0 );\n"
"}");
ASSERT_EQUALS("", errout.str());
check("const char f()\n"
"{\n"
" const char var[6] = 'index';\n"
" const char i = index(var, 0);\n"
" return i;\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (style) Obsolete function 'index' called. It is recommended to use the function 'strchr' instead.\n",
ASSERT_EQUALS("[test.cpp:2]: (style) Obsolete function 'index' called. It is recommended to use the function 'strchr' instead.\n", errout.str());
}
voidtestrindex() {
check("void f()\n"
"{\n"
" int rindex( 0 );\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"
" const char var[7] = 'rindex';\n"
" print(rindex(var, 0));\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (style) Obsolete function 'rindex' called. It is recommended to use the function 'strrchr' instead.\n", errout.str());
}
voidtestvar() {
check("class Fred {\n"
"public:\n"
" Fred() : index(0) { }\n"
" int index;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
voidtestgets() {
check("void f()\n"
"{\n"
" char *x = gets();\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n", errout.str());
check("void f()\n"
"{\n"
" foo(x, gets());\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n", errout.str());
}
voidtestalloca() {
check("void f()\n"
"{\n"
" char *x = alloca(10);\n"
"}\n", "test.cpp"); // #4382 - there are no VLAs in C++
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"
" char *x = alloca(10);\n"
"}\n", "test.c");
ASSERT_EQUALS("[test.c:3]: (style) Obsolete function 'alloca' called. In C99 and later it is recommended to use a variable length array instead.\n", errout.str());
}
// ticket #3121
voidtest_declared_function() {
check("int ftime ( int a )\n"
"{\n"
" return a;\n"
"}\n"
"int main ()\n"
"{\n"
" int b ; b = ftime ( 1 ) ;\n"
" return 0 ;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// test std::gets
voidtest_std_gets() {
check("void f(char * str)\n"
"{\n"
" char *x = std::gets(str);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n", errout.str());
}
// multiple use
voidtest_multiple() {
check("void f(char * str)\n"
"{\n"
" char *x = std::gets(str);\n"
" usleep( 1000 );\n"
"}");
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());
}
voidtest_c_declaration() {
check("char * gets ( char * c ) ;\n"
"int main ()\n"
"{\n"
" char s [ 10 ] ;\n"
" gets ( s ) ;\n"
"}");
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"
"}");
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());