Skip to content

Commit 59e4eef

Browse files
Fix #13199 nullptr dereference in checkInnerScope() (danmar#6878)
1 parent d00dbe0 commit 59e4eef

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/checkother.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,10 +1254,12 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us
12541254
}
12551255
if (ftok->function()) {
12561256
const std::list<Variable> &argvars = ftok->function()->argumentList;
1257-
const Variable *argvar = ftok->function()->getArgumentVar(argn);
1258-
if (!std::all_of(argvars.cbegin(), argvars.cend(), [&](const Variable &other) {
1259-
return &other == argvar || !mayDependOn(other.valueType(), argvar->valueType());
1260-
})) return false;
1257+
if (const Variable* argvar = ftok->function()->getArgumentVar(argn)) {
1258+
if (!std::all_of(argvars.cbegin(), argvars.cend(), [&](const Variable& other) {
1259+
return &other == argvar || !mayDependOn(other.valueType(), argvar->valueType());
1260+
}))
1261+
return false;
1262+
}
12611263
}
12621264
}
12631265
}

test/testother.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,14 @@ class TestOther : public TestFixture {
18431843
" printf(\"result: %d\\n\", msg);\n"
18441844
"}\n");
18451845
ASSERT_EQUALS("", errout_str());
1846+
1847+
check("void g(const char* format, ...);\n"
1848+
"void f(bool b) {\n"
1849+
" const char* s = \"abc\";\n"
1850+
" if (b)\n"
1851+
" g(\"%d %s\", 1, s);\n"
1852+
"}\n");
1853+
ASSERT_EQUALS("[test.cpp:3]: (style) The scope of the variable 's' can be reduced.\n", errout_str());
18461854
}
18471855

18481856
#define checkOldStylePointerCast(...) checkOldStylePointerCast_(__FILE__, __LINE__, __VA_ARGS__)

0 commit comments

Comments
 (0)