diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 9fbf48488..f1e81cbf3 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1162,7 +1162,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list 0) { - for (const Token *tok2 = tok->tokAt(2); tok2 != end; tok2 = tok2->next()) { + for (const Token *tok2 = tok->tokAt(2); tok2 && tok2 != end; tok2 = tok2->next()) { if (notvar(tok2, varid)) { addtoken(&rettail, tok2, "!var"); break; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 3a218d505..1d7205c6e 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -350,6 +350,8 @@ private: // #1879 non regression test case TEST_CASE(trac1879); + + TEST_CASE(garbageCode); } @@ -3805,6 +3807,11 @@ private: ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: a\n", errout.str()); } + void garbageCode() { + check("void h(int l) {\n" + " while\n" // Don't crash (#3870) + "}"); + } };