Skip to content

Commit 4eade06

Browse files
committed
impl and tests
1 parent 637d5be commit 4eade06

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/checkuninitvar.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ bool CheckUninitVarImpl::checkScopeForVariable(const Token *tok, const Variable&
834834
}
835835
}
836836
}
837-
if (Token::simpleMatch(parent->astParent(), "=") && astIsLHS(parent)) {
837+
if (parent->astParent() && parent->astParent()->isAssignmentOp() && astIsLHS(parent)) {
838838
const Token *eq = parent->astParent();
839839
if (const Token *errorToken = checkExpr(eq->astOperand2(), var, *alloc, number_of_if==0)) {
840840
if (!suppressErrors)
@@ -1295,7 +1295,17 @@ 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+
}
1306+
}
12981307
}
1308+
12991309
}
13001310

13011311
// Initialize reference variable

test/testuninitvar.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,25 @@ class TestUninitVar : public TestFixture {
22022202
" return i;\n"
22032203
"}\n");
22042204
ASSERT_EQUALS("", errout_str());
2205+
2206+
2207+
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"
2213+
"}\n");
2214+
ASSERT_EQUALS("", errout_str());
2215+
2216+
checkUninitVar("void f() {\n"
2217+
" char *buf = (char *)malloc(1);\n"
2218+
" if (!buf)\n"
2219+
" return NULL;\n"
2220+
" buf += buf[0];\n"
2221+
" free(buf);\n"
2222+
"}\n");
2223+
ASSERT_EQUALS("[test.cpp:5:15]: (error) Memory is allocated but not initialized: buf[0] [uninitdata]\n", errout_str());
22052224
}
22062225

22072226
// class / struct..

0 commit comments

Comments
 (0)