From 5caab6ba1043aa10701c3dabdbb6817a5b109963 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Fri, 6 Jul 2012 09:03:33 -0700 Subject: [PATCH] Fixed crash on garbage code (#3870) --- lib/checkmemoryleak.cpp | 2 +- test/testmemleak.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) 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) + "}"); + } };