Skip to content

Commit a951d34

Browse files
committed
Fixed ticket danmar#3480 (segmentation fault of cppcheck)
1 parent 1943270 commit a951d34

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

lib/executionpath.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -300,29 +300,27 @@ void ExecutionPath::checkScope(const Token *tok, std::list<ExecutionPath *> &che
300300
// bailout used variables in '; FOREACH ( .. ) { .. }'
301301
else if (tok->str() != "if" && Token::Match(tok->previous(), "[;{}] %var% (")) {
302302
// goto {
303-
const Token *tok2 = tok->next()->link();
304-
if (tok2 && tok2->str() == ")") {
305-
tok2 = tok2->next();
306-
if (tok2 && tok2->str() == "{") {
307-
// goto "}"
308-
tok2 = tok2->link();
309-
310-
// bail out all variables used in "{ .. }"
311-
for (; tok && tok != tok2; tok = tok->next()) {
312-
if (tok->varId())
313-
ExecutionPath::bailOutVar(checks, tok->varId());
314-
}
303+
const Token *tok2 = tok->next()->link()->next();
304+
if (tok2 && tok2->str() == "{") {
305+
// goto "}"
306+
tok2 = tok2->link();
307+
308+
// bail out all variables used in "{ .. }"
309+
for (; tok && tok != tok2; tok = tok->next()) {
310+
if (tok->varId())
311+
ExecutionPath::bailOutVar(checks, tok->varId());
315312
}
316313
}
317314
}
318315

319316
// .. ) { ... } => bail out
320-
if (Token::simpleMatch(tok, ") {")) {
317+
if (tok->str() == ")" && tok->next() && tok->next()->str() == "{") {
321318
ExecutionPath::bailOut(checks);
322319
return;
323320
}
324321

325-
if (Token::Match(tok, "abort|exit (")) {
322+
if ((tok->str() == "abort" || tok->str() == "exit") &&
323+
tok->next() && tok->next()->str() == "(") {
326324
ExecutionPath::bailOut(checks);
327325
return;
328326
}
@@ -360,12 +358,12 @@ void ExecutionPath::checkScope(const Token *tok, std::list<ExecutionPath *> &che
360358
continue;
361359
}
362360

363-
if (tok->str() == "if") {
361+
if (tok->str() == "if" && tok->next() && tok->next()->str() == "(") {
364362
// what variable ids should the numberOfIf be counted for?
365363
std::set<unsigned int> countif;
366364

367365
std::list<ExecutionPath *> newchecks;
368-
while (tok->str() == "if") {
366+
while (tok->str() == "if" && tok->next() && tok->next()->str() == "(") {
369367
// goto "("
370368
tok = tok->next();
371369

@@ -377,12 +375,12 @@ void ExecutionPath::checkScope(const Token *tok, std::list<ExecutionPath *> &che
377375
}
378376

379377
// goto ")"
380-
tok = tok ? tok->link() : 0;
378+
tok = tok->link();
381379

382380
// goto "{"
383-
tok = tok ? tok->next() : 0;
381+
tok = tok->next();
384382

385-
if (!Token::simpleMatch(tok, "{")) {
383+
if (!tok || tok->str() != "{") {
386384
ExecutionPath::bailOut(checks);
387385
ExecutionPath::bailOut(newchecks);
388386
return;
@@ -395,12 +393,12 @@ void ExecutionPath::checkScope(const Token *tok, std::list<ExecutionPath *> &che
395393
tok = tok->link();
396394

397395
// there is no else => break out
398-
if (Token::Match(tok, "} !!else"))
396+
if (!tok->next() || tok->next()->str() != "else")
399397
break;
400398

401399
// parse next "if"..
402400
tok = tok->tokAt(2);
403-
if (tok->str() == "if")
401+
if (tok && tok->str() == "if")
404402
continue;
405403

406404
// there is no "if"..

test/testuninitvar.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,13 @@ class TestUninitVar : public TestFixture {
527527
" return x() ? i : 0;\n"
528528
"}\n");
529529
TODO_ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: i\n", "", errout.str());
530+
531+
// Ticket #3480 - Don't crash garbage code
532+
checkUninitVar("int f()\n"
533+
"{\n"
534+
" return if\n"
535+
"}\n");
536+
ASSERT_EQUALS("", errout.str());
530537
}
531538

532539

0 commit comments

Comments
 (0)