Skip to content

Commit 32ab9d1

Browse files
Fix #14164 FN nullPointerRedundantCheck with ternary in function call (#7894)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 44b2b45 commit 32ab9d1

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

lib/checkcondition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,7 @@ void CheckCondition::checkCompareValueOutOfTypeRange()
19861986
default:
19871987
break;
19881988
}
1989-
if (bits == 0 || bits >= 64)
1989+
if (bits == 0 || bits >= 63)
19901990
continue;
19911991

19921992
const auto typeMinValue = (typeTok->valueType()->sign == ValueType::Sign::UNSIGNED) ? 0 : (-(1LL << (bits-1)));

lib/forwardanalyzer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ namespace {
208208
bool checkThen, checkElse;
209209
std::tie(checkThen, checkElse) = evalCond(condTok);
210210
if (!checkThen && !checkElse) {
211-
if (!traverseUnknown && stopOnCondition(condTok) && stopUpdates()) {
211+
if (!traverseUnknown && stopOnCondition(condTok) && tok->str() != "?" && stopUpdates()) {
212212
return Progress::Continue;
213213
}
214214
checkThen = true;

test/testnullpointer.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,9 @@ class TestNullPointer : public TestFixture {
14041404
" if (x) p = q;\n"
14051405
" if (y ? p->x : p->y) { }\n"
14061406
"}");
1407-
TODO_ASSERT_EQUALS("[test.cpp:4]: (warning) Possible null pointer dereference: p\n", "", errout_str());
1407+
ASSERT_EQUALS("[test.cpp:4:13]: (warning) Possible null pointer dereference: p [nullPointer]\n"
1408+
"[test.cpp:4:20]: (warning) Possible null pointer dereference: p [nullPointer]\n",
1409+
errout_str());
14081410
}
14091411

14101412
void nullpointer21() { // #4038 - fp: if (x) p=q; else return;
@@ -3373,7 +3375,7 @@ class TestNullPointer : public TestFixture {
33733375
" if (!p) {}\n"
33743376
" return q ? p->x : 0;\n"
33753377
"}");
3376-
TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p.\n", "", errout_str());
3378+
ASSERT_EQUALS("[test.cpp:2:9] -> [test.cpp:3:16]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. [nullPointerRedundantCheck]\n", errout_str());
33773379

33783380
check("int f(ABC *p) {\n" // FP : return &&
33793381
" if (!p) {}\n"
@@ -3393,6 +3395,20 @@ class TestNullPointer : public TestFixture {
33933395
" pointer = func(sizeof pointer[0]);\n"
33943396
"}");
33953397
ASSERT_EQUALS("", errout_str());
3398+
3399+
check("struct T {\n" // #14164
3400+
" T* next;\n"
3401+
" char op;\n"
3402+
"};\n"
3403+
"void h(int, char);\n"
3404+
"void g(const T* tok, bool b) {\n"
3405+
" if (tok->op == '<') {\n"
3406+
" while ((tok = tok->next) && tok->op != '>') {}\n"
3407+
" }\n"
3408+
" h(b ? 1 : 0, tok->op);\n"
3409+
"}\n");
3410+
ASSERT_EQUALS("[test.cpp:8:21] -> [test.cpp:10:18]: (warning) Either the condition 'tok=tok->next' is redundant or there is possible null pointer dereference: tok. [nullPointerRedundantCheck]\n",
3411+
errout_str());
33963412
}
33973413

33983414
// Test CheckNullPointer::nullConstantDereference

0 commit comments

Comments
 (0)