Skip to content

Commit 1d9a990

Browse files
committed
Fixed 3133 (Improve Check: Found obsolete function)
- add check for std::gets - improve check when multiple obsolete functions are used - remove false positive (declared functions)
1 parent 9270b84 commit 1d9a990

2 files changed

Lines changed: 55 additions & 5 deletions

File tree

lib/checkobsoletefunctions.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ void CheckObsoleteFunctions::obsoleteFunctions()
4545

4646
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
4747
{
48-
if (tok->isName() && tok->varId()==0 && tok->strAt(1) == "(" && !Token::Match(tok->previous(), ".|::|:|,"))
48+
49+
if (tok->isName() && tok->varId()==0 && tok->strAt(1) == "(" &&
50+
(!Token::Match(tok->previous(), ".|::|:|,") || Token::simpleMatch(tok->previous()->previous(), "std :: gets")))
4951
{
5052
// function declaration?
51-
if (symbolDatabase->findFunctionByToken(tok))
53+
if ((tok->previous() && (tok->previous()->str() == "*" || tok->previous()->isName()))
54+
|| symbolDatabase->findFunctionByToken(tok))
5255
{
5356
_obsoleteStandardFunctions.erase(tok->str());
5457
_obsoletePosixFunctions.erase(tok->str());
@@ -61,7 +64,6 @@ void CheckObsoleteFunctions::obsoleteFunctions()
6164
// If checking an old code base it might be uninteresting to update obsolete functions.
6265
// Therefore this is "information"
6366
reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);
64-
break;
6567
}
6668
else
6769
{
@@ -73,7 +75,6 @@ void CheckObsoleteFunctions::obsoleteFunctions()
7375
// If checking an old code base it might be uninteresting to update obsolete functions.
7476
// Therefore this is "information"
7577
reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);
76-
break;
7778
}
7879
}
7980
if (_settings->c99)
@@ -82,7 +83,6 @@ void CheckObsoleteFunctions::obsoleteFunctions()
8283
if (it != _obsoleteC99Functions.end())
8384
{
8485
reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);
85-
break;
8686
}
8787
}
8888
}

test/testobsoletefunctions.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ class TestObsoleteFunctions : public TestFixture
5353

5454
// declared function ticket #3121
5555
TEST_CASE(test_declared_function);
56+
57+
// test std::gets
58+
TEST_CASE(test_std_gets);
59+
60+
// multiple use of obsolete functions
61+
TEST_CASE(test_multiple);
62+
63+
// c declared function
64+
TEST_CASE(test_c_declaration);
65+
5666
}
5767

5868
void check(const char code[])
@@ -242,6 +252,46 @@ class TestObsoleteFunctions : public TestFixture
242252
"}\n");
243253
ASSERT_EQUALS("", errout.str());
244254
}
255+
256+
// test std::gets
257+
void test_std_gets()
258+
{
259+
check("void f(char * str)\n"
260+
"{\n"
261+
" char *x = std::gets(str);\n"
262+
"}\n");
263+
ASSERT_EQUALS("[test.cpp:3]: (style) Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n", errout.str());
264+
}
265+
266+
// multiple use
267+
void test_multiple()
268+
{
269+
check("void f(char * str)\n"
270+
"{\n"
271+
" char *x = std::gets(str);\n"
272+
" usleep( 1000 );\n"
273+
"}\n");
274+
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());
275+
}
276+
277+
void test_c_declaration()
278+
{
279+
check("char * gets ( char * c ) ;\n"
280+
"int main ()\n"
281+
"{\n"
282+
" char s [ 10 ] ;\n"
283+
" gets ( s ) ;\n"
284+
"}\n");
285+
ASSERT_EQUALS("", errout.str());
286+
287+
check("int getcontext(ucontext_t *ucp);\n"
288+
"int f (ucontext_t *ucp)\n"
289+
"{\n"
290+
" getcontext ( ucp ) ;\n"
291+
"}\n");
292+
ASSERT_EQUALS("", errout.str());
293+
}
294+
245295
};
246296

247297
REGISTER_TEST(TestObsoleteFunctions)

0 commit comments

Comments
 (0)