Skip to content

Commit d9eacae

Browse files
authored
Fix issue 9842: ValueFlow: wrong handling of ?, seems to think that the whole expression is a condition (danmar#2821)
1 parent 2e24cc1 commit d9eacae

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/valueflow.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,9 +2033,12 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, SymbolDatabase *symbo
20332033
Token::Match(tok, "%oror%|&& %name% %oror%|&&|)")) {
20342034
vartok = tok->next();
20352035
num = 0;
2036-
} else if (Token::Match(tok, "[!?]") && Token::Match(tok->astOperand1(), "%name%")) {
2036+
} else if (Token::simpleMatch(tok, "!") && Token::Match(tok->astOperand1(), "%name%")) {
20372037
vartok = tok->astOperand1();
20382038
num = 0;
2039+
} else if (Token::simpleMatch(tok->astParent(), "?") && Token::Match(tok, "%name%")) {
2040+
vartok = tok;
2041+
num = 0;
20392042
} else {
20402043
continue;
20412044
}
@@ -2046,7 +2049,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, SymbolDatabase *symbo
20462049
if (varid == 0U || !var)
20472050
continue;
20482051

2049-
if (tok->str() == "?" && tok->isExpandedMacro()) {
2052+
if (Token::simpleMatch(tok->astParent(), "?") && tok->astParent()->isExpandedMacro()) {
20502053
if (settings->debugwarnings)
20512054
bailout(tokenlist, errorLogger, tok, "variable " + var->name() + ", condition is defined in macro");
20522055
continue;

test/testnullpointer.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class TestNullPointer : public TestFixture {
100100
TEST_CASE(nullpointer57); // #9751
101101
TEST_CASE(nullpointer58); // #9807
102102
TEST_CASE(nullpointer59); // #9897
103+
TEST_CASE(nullpointer60); // #9842
103104
TEST_CASE(nullpointer_addressOf); // address of
104105
TEST_CASE(nullpointerSwitch); // #2626
105106
TEST_CASE(nullpointer_cast); // #4692
@@ -1878,6 +1879,19 @@ class TestNullPointer : public TestFixture {
18781879
ASSERT_EQUALS("", errout.str());
18791880
}
18801881

1882+
void nullpointer60() {
1883+
check("void f(){\n"
1884+
" char uuid[128];\n"
1885+
" char *s1;\n"
1886+
" memset(uuid, 0, sizeof(uuid));\n"
1887+
" s1 = strchr(uuid, '=');\n"
1888+
" s1 = s1 ? s1 + 1 : &uuid[5];\n"
1889+
" if (!strcmp(\"00000000000000000000000000000000\", s1) )\n"
1890+
" return;\n"
1891+
"}\n");
1892+
ASSERT_EQUALS("", errout.str());
1893+
}
1894+
18811895
void nullpointer_addressOf() { // address of
18821896
check("void f() {\n"
18831897
" struct X *x = 0;\n"

0 commit comments

Comments
 (0)