Add some tests (#4211)

* Add test for #10152

* Add test for #9773

* Fix test

* Add test for #7529

* Add test for #6371

* Add test for #6475

* Format

* Format

* Fix test

* Remove duplicate test

* Add valueflow test

* Rebuild
This commit is contained in:
chrchr-github 2022-06-14 09:54:45 +02:00 committed by GitHub
parent 343d04feb4
commit 6f5a5fd947
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 2 deletions

View File

@ -3571,6 +3571,19 @@ private:
" for (const auto& s : a) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("std::vector<char*> f(const std::vector<std::string>& args) {\n" // #9773
" std::vector<char*> cargs;\n"
" for (const auto& a : args) {\n"
" cargs.push_back(const_cast<char*>(a.data()));\n"
" }\n"
" return cargs;\n"
"}\n"
"void g() {\n"
" std::vector<char*> cargs = f({ \"0\", \"0\" });\n"
" (void)cargs;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:4] -> [test.cpp:3] -> [test.cpp:1] -> [test.cpp:4] -> [test.cpp:9] -> [test.cpp:9] -> [test.cpp:10]: (error) Using object that is a temporary.\n", errout.str());
}
void danglingLifetimeBorrowedMembers()

View File

@ -656,6 +656,12 @@ private:
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(std::string a, std::string b) {\n" // #7529
" const std::string s = \" x \" + a;\n"
" +\" y = \" + b;\n"
"}\n", /*inconclusive*/ true);
ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Found suspicious operator '+', result is not used.\n", errout.str());
}
void vardecl() {

View File

@ -2041,6 +2041,20 @@ private:
" return NewT(s);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("typedef struct s { char* str; } attr_t;\n" // #10152
"attr_t* f(int type) {\n"
" attr_t a;\n"
" switch (type) {\n"
" case 1:\n"
" a.str = strdup(\"?\");\n"
" break;\n"
" default:\n"
" return NULL;\n"
" }\n"
" return g(&a);\n"
"}\n");
TODO_ASSERT_EQUALS("", "[test.cpp:9]: (error) Memory leak: a.str\n", errout.str());
}
void ifelse() {

View File

@ -5978,8 +5978,8 @@ private:
errout.str());
}
void duplicateExpression16() { //#10569
check("void f(const std::string& a) {\n"
void duplicateExpression16() {
check("void f(const std::string& a) {\n" //#10569
" if ((a == \"x\") ||\n"
" (a == \"42\") ||\n"
" (a == \"y\") ||\n"
@ -6001,6 +6001,14 @@ private:
"[test.cpp:7] -> [test.cpp:9]: (style) Same expression 'a==\"42\"' found multiple times in chain of '||' operators.\n"
"[test.cpp:13] -> [test.cpp:16]: (style) Same expression 'a==\"42\"' found multiple times in chain of '||' operators.\n",
errout.str());
check("void f(const char* s) {\n" // #6371
" if (*s == '\x0F') {\n"
" if (!s[1] || !s[2] || !s[1])\n"
" break;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Same expression '!s[1]' found multiple times in chain of '||' operators.\n", errout.str());
}
void duplicateExpressionLoop() {

View File

@ -5359,6 +5359,14 @@ private:
" if (received[sig]) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
valueFlowUninit("void increment(int& i) { ++i; }\n" // #6475
"int f() {\n"
" int n;\n"
" increment(n);\n"
" return n;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (error) Uninitialized variable: i\n", errout.str());
}
void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value