Skip to content

Commit fe75686

Browse files
committed
Ticket cppcheck-opensource#5203: Don't crash when checking buffer overrun for invalid code.
1 parent 2b7835c commit fe75686

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

lib/checkbufferoverrun.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ static bool bailoutIfSwitch(const Token *tok, const unsigned int varid)
229229
const Token* end = tok->linkAt(1)->linkAt(1);
230230
if (Token::simpleMatch(end, "} else {")) // scan the else-block
231231
end = end->linkAt(2);
232+
if (Token::simpleMatch(end, "{")) // Ticket #5203: Invalid code, bailout
233+
return true;
232234
for (; tok != end; tok = tok->next()) {
233235
// If scanning a "if" block then bailout for "break"
234236
if (is_if && (tok->str() == "break" || tok->str() == "continue"))

test/testbufferoverrun.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ class TestBufferOverrun : public TestFixture {
240240
TEST_CASE(crash2); // Ticket #2607 - crash
241241
TEST_CASE(crash3); // Ticket #3034 - crash
242242

243+
TEST_CASE(garbage1); // Ticket #5203
244+
243245
TEST_CASE(executionPaths1);
244246
TEST_CASE(executionPaths2);
245247
TEST_CASE(executionPaths3); // no FP for function parameter
@@ -3633,6 +3635,9 @@ class TestBufferOverrun : public TestFixture {
36333635
"}");
36343636
}
36353637

3638+
void garbage1() { // Ticket #5203
3639+
check("int f ( int* r ) { { int s[2] ; f ( s ) ; if ( ) } }");
3640+
}
36363641

36373642
void epcheck(const char code[], const char filename[] = "test.cpp") {
36383643
// Clear the error buffer..

0 commit comments

Comments
 (0)