Skip to content

Commit 7a7c28c

Browse files
authored
Fix #14729: False positive: uninitdata reported for ternary operator that does not dereference buffer (#8550)
1 parent 7bfa458 commit 7a7c28c

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

lib/checkuninitvar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ static bool isVariableUsed(const Token *tok, const Variable& var)
383383
return isVariableUsed(tok->astOperand1(),var) || isVariableUsed(tok->astOperand2(),var);
384384
if (tok->varId() != var.declarationId())
385385
return false;
386-
if (!var.isArray())
386+
if (!var.isArray() && !var.isPointer())
387387
return true;
388388

389389
const Token *parent = tok->astParent();

test/testuninitvar.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ class TestUninitVar : public TestFixture {
8080
TEST_CASE(uninitvar_unconditionalTry);
8181
TEST_CASE(uninitvar_funcptr); // #6404
8282
TEST_CASE(uninitvar_operator); // #6680
83-
TEST_CASE(uninitvar_ternaryexpression); // #4683
83+
TEST_CASE(uninitvar_ternaryexpression1); // #4683
84+
TEST_CASE(uninitvar_ternaryexpression2); // #14729
8485
TEST_CASE(uninitvar_pointertoarray);
8586
TEST_CASE(uninitvar_cpp11ArrayInit); // #7010
8687
TEST_CASE(uninitvar_rangeBasedFor); // #7078
@@ -5447,7 +5448,7 @@ class TestUninitVar : public TestFixture {
54475448
ASSERT_EQUALS("", errout_str());
54485449
}
54495450

5450-
void uninitvar_ternaryexpression() { // #4683
5451+
void uninitvar_ternaryexpression1() { // #4683
54515452
checkUninitVar("struct B { int asd; };\n"
54525453
"int f() {\n"
54535454
" int a=0;\n"
@@ -5461,6 +5462,14 @@ class TestUninitVar : public TestFixture {
54615462
ASSERT_EQUALS("", errout_str());
54625463
}
54635464

5465+
void uninitvar_ternaryexpression2() { // #14729
5466+
checkUninitVar("void f(bool b, int i) {\n"
5467+
" int *p = (int*) malloc(4);\n"
5468+
" int *q = b ? p : &i;\n"
5469+
"}\n");
5470+
ASSERT_EQUALS("", errout_str());
5471+
}
5472+
54645473
void uninitvar_rangeBasedFor() {
54655474
checkUninitVar("void function(Entry& entry) {\n" // #7078
54665475
" for (auto* expr : entry.exprs) {\n"

0 commit comments

Comments
 (0)