Skip to content

Commit ba8cca8

Browse files
committed
cppcheck-opensource#4706 fix crash when a struct member is used as first argument. Replaced Token::nexArgument with %any% in Token::Match call. Added unittests in testing Token::nexArgument.
1 parent c5d636c commit ba8cca8

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

lib/checkbufferoverrun.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2248,7 +2248,7 @@ void CheckBufferOverrun::writeOutsideBufferSize()
22482248
for (std::size_t i = 0; i < functions; ++i) {
22492249
const Scope * scope = symbolDatabase->functionScopes[i];
22502250
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
2251-
if (Token::Match(tok, "pwrite|write (") && Token::Match(tok->tokAt(2)->nextArgument(), "%str% , %num%")) {
2251+
if (Token::Match(tok, "pwrite|write (") && Token::Match(tok->tokAt(2), "%any% , %str% , %num%")) {
22522252
const std::string & functionName(tok->str());
22532253
tok = tok->tokAt(4); // set tokenptr to %str% parameter
22542254
const std::size_t stringLength = Token::getStrLength(tok);

test/testbufferoverrun.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4104,6 +4104,16 @@ class TestBufferOverrun : public TestFixture {
41044104
"write(1, \"Dump string \\n\", 10);\n"
41054105
"}");
41064106
ASSERT_EQUALS("", errout.str());
4107+
4108+
// #4706 avoid crashing when a struct member is used as first argument
4109+
check("static struct {\n"
4110+
" int i[2];\n"
4111+
"} p;\n"
4112+
"void foo()\n"
4113+
"{\n"
4114+
" write(p.i[1], \"\", 1);\n"
4115+
"}");
4116+
ASSERT_EQUALS("", errout.str());
41074117
}
41084118
};
41094119

test/testtoken.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,16 @@ class TestToken : public TestFixture {
279279
void nextArgument() const {
280280
givenACodeSampleToTokenize example1("foo(1, 2, 3, 4);");
281281
ASSERT_EQUALS(true, Token::simpleMatch(example1.tokens()->tokAt(2)->nextArgument(), "2 , 3"));
282+
ASSERT_EQUALS(true, Token::simpleMatch(example1.tokens()->tokAt(4)->nextArgument(), "3 , 4"));
282283

283284
givenACodeSampleToTokenize example2("foo();");
284285
ASSERT_EQUALS(true, example2.tokens()->tokAt(2)->nextArgument() == 0);
285286

286287
givenACodeSampleToTokenize example3("foo(bar(a, b), 2, 3);");
287288
ASSERT_EQUALS(true, Token::simpleMatch(example3.tokens()->tokAt(2)->nextArgument(), "2 , 3"));
289+
290+
givenACodeSampleToTokenize example4("foo(x.i[1], \"\", 3);");
291+
ASSERT_EQUALS(true, Token::simpleMatch(example4.tokens()->tokAt(2)->nextArgument(), "\"\" , 3"));
288292
}
289293

290294
void eraseTokens() const {

0 commit comments

Comments
 (0)