Merge pull request #2704 from rikardfalkeborn/9635-memleak-with-comma-operator-in-if-statement

Fix #9635 (FP: Memory leak with comma operator in if-statement)
This commit is contained in:
Daniel Marjamäki 2020-07-13 20:08:57 +02:00 committed by GitHub
commit 8ba714373f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -465,7 +465,11 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
// Recursively scan variable comparisons in condition // Recursively scan variable comparisons in condition
std::stack<const Token *> tokens; std::stack<const Token *> tokens;
tokens.push(tok->next()->astOperand2()); // Skip expressions before commas
const Token * astOperand2AfterCommas = tok->next()->astOperand2();
while (Token::simpleMatch(astOperand2AfterCommas, ","))
astOperand2AfterCommas = astOperand2AfterCommas->astOperand2();
tokens.push(astOperand2AfterCommas);
while (!tokens.empty()) { while (!tokens.empty()) {
const Token *tok3 = tokens.top(); const Token *tok3 = tokens.top();
tokens.pop(); tokens.pop();

View File

@ -134,6 +134,7 @@ private:
TEST_CASE(ifelse13); // #8392 TEST_CASE(ifelse13); // #8392
TEST_CASE(ifelse14); // #9130 - if (x == (char*)NULL) TEST_CASE(ifelse14); // #9130 - if (x == (char*)NULL)
TEST_CASE(ifelse15); // #9206 - if (global_ptr = malloc(1)) TEST_CASE(ifelse15); // #9206 - if (global_ptr = malloc(1))
TEST_CASE(ifelse16); // #9635 - if (p = malloc(4), p == NULL)
// switch // switch
TEST_CASE(switch1); TEST_CASE(switch1);
@ -1484,6 +1485,26 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void ifelse16() { // #9635
check("void f(void) {\n"
" char *p;\n"
" if(p = malloc(4), p == NULL)\n"
" return;\n"
" free(p);\n"
" return;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(void) {\n"
" char *p, q;\n"
" if(p = malloc(4), q = 1, p == NULL)\n"
" return;\n"
" free(p);\n"
" return;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void switch1() { void switch1() {
check("void f() {\n" check("void f() {\n"
" char *p = 0;\n" " char *p = 0;\n"