Skip to content

Commit c2584aa

Browse files
committed
danmar#6303 crash in CheckBufferOverrun. Add check on loop variable in CheckBufferOverrun::checkScope().
1 parent 7a6cd54 commit c2584aa

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/checkbufferoverrun.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
517517

518518
const bool isPortabilityEnabled = _settings->isEnabled("portability");
519519

520-
for (const Token* const end = tok->scope()->classEnd; tok != end; tok = tok->next()) {
520+
for (const Token* const end = tok->scope()->classEnd; tok && tok != end; tok = tok->next()) {
521521
if (declarationId != 0 && Token::Match(tok, "%varid% = new|malloc|realloc", declarationId)) {
522522
// Abort
523523
break;

test/testbufferoverrun.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ class TestBufferOverrun : public TestFixture {
316316
TEST_CASE(writeOutsideBufferSize)
317317

318318
TEST_CASE(negativeMemoryAllocationSizeError) // #389
319+
320+
TEST_CASE(garbage1) // #6303
319321
}
320322

321323

@@ -4262,6 +4264,14 @@ class TestBufferOverrun : public TestFixture {
42624264
"}\n");
42634265
ASSERT_EQUALS("[test.cpp:4]: (error) Memory allocation size is negative.\n", errout.str());
42644266
}
4267+
4268+
void garbage1() {
4269+
check("void foo() {\n"
4270+
"char *a = malloc(10);\n"
4271+
"a[0]\n"
4272+
"}\n");
4273+
ASSERT_EQUALS("", errout.str());
4274+
}
42654275
};
42664276

42674277
REGISTER_TEST(TestBufferOverrun)

0 commit comments

Comments
 (0)