Skip to content

Commit 4d9473b

Browse files
committed
handle ++/-- and avoid ctu stuff
1 parent 4eade06 commit 4d9473b

2 files changed

Lines changed: 37 additions & 14 deletions

File tree

lib/checkuninitvar.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,17 +1295,28 @@ const Token* CheckUninitVarImpl::isVariableUsage(const Token *vartok, const Libr
12951295
}
12961296
if (alloc != NO_ALLOC && astIsRhs(valueExpr))
12971297
return nullptr;
1298-
} else if (tok->astParent() && tok->astParent()->isAssignmentOp()) {
1299-
// all variables in a compound assignment get read, so NO_ALLOC is
1300-
// never acceptable
1301-
if (alloc != NO_ALLOC) {
1302-
// if its not a pointer or array, or not dereferenced it is safe
1303-
if (!(pointer || alloc == ARRAY) || !derefValue) {
1304-
return nullptr;
1305-
}
1298+
} else if (tok->astParent() && (tok->astParent()->isAssignmentOp() || tok->astParent()->isIncDecOp())) {
1299+
// NO_ALLOC -> no matter what we read the uninitialized memory.
1300+
// pointer/array -> safe, as long as we don't dereference
1301+
//
1302+
// sometimes "pointer" and "alloc == ARRAY" are used for things
1303+
// that aren't actually pointers or arrays.
1304+
//
1305+
// this test
1306+
// ctu("void increment(int& i) { ++i; }\n" // #6475
1307+
// uses the callback which hardcodes pointer = true and alloc = ARRAY
1308+
// though int& isn't a pointer or an array, and we expect this
1309+
// function to not return nullptr even though i is not dereferenced
1310+
bool isPtr = pointer;
1311+
bool isArr = alloc == ARRAY;
1312+
if (vartok && vartok->variable()) {
1313+
isPtr = vartok->variable()->isPointer();
1314+
isArr = vartok->variable()->isArray();
1315+
}
1316+
if ((alloc != NO_ALLOC) && ((isPtr || isArr) && !derefValue)) {
1317+
return nullptr;
13061318
}
13071319
}
1308-
13091320
}
13101321

13111322
// Initialize reference variable

test/testuninitvar.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,11 +2205,9 @@ class TestUninitVar : public TestFixture {
22052205

22062206

22072207
checkUninitVar("void f() {\n"
2208-
" char *buf = (char *)malloc(1);\n"
2209-
" if (!buf)\n"
2210-
" return NULL;\n"
2211-
" buf += 1;\n"
2212-
" free(buf - 1);\n"
2208+
" char *p = new char;\n"
2209+
" p += 1;\n"
2210+
" delete (p - 1);\n"
22132211
"}\n");
22142212
ASSERT_EQUALS("", errout_str());
22152213

@@ -2221,6 +2219,20 @@ class TestUninitVar : public TestFixture {
22212219
" free(buf);\n"
22222220
"}\n");
22232221
ASSERT_EQUALS("[test.cpp:5:15]: (error) Memory is allocated but not initialized: buf[0] [uninitdata]\n", errout_str());
2222+
2223+
checkUninitVar("void g() {\n"
2224+
" int* p = new int;\n"
2225+
" p++;\n"
2226+
" delete (p - 1);\n"
2227+
"}\n");
2228+
ASSERT_EQUALS("", errout_str());
2229+
2230+
checkUninitVar("void g() {\n"
2231+
" int* p = new int;\n"
2232+
" ++p; // FP\n"
2233+
" delete (p - 1);\n"
2234+
"}\n");
2235+
ASSERT_EQUALS("", errout_str());
22242236
}
22252237

22262238
// class / struct..

0 commit comments

Comments
 (0)