diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index e3aa623ef..64fd7bed1 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2488,7 +2488,7 @@ bool CheckMemoryLeakStructMember::isMalloc(const Variable *variable) { const unsigned int declarationId(variable->declarationId()); bool alloc = false; - for (const Token *tok2 = variable->nameToken(); tok2 != variable->scope()->classEnd; tok2 = tok2->next()) { + for (const Token *tok2 = variable->nameToken(); tok2 && tok2 != variable->scope()->classEnd; tok2 = tok2->next()) { if (Token::Match(tok2, "= %varid% [;=]", declarationId)) { return false; } else if (Token::Match(tok2, "%varid% = malloc|kmalloc (", declarationId)) { diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 963a8259f..472309151 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -5135,6 +5135,10 @@ private: // local struct variable TEST_CASE(localvars); + + // Segmentation fault in CheckMemoryLeakStructMember + TEST_CASE(trac5030); + } void err() { @@ -5423,6 +5427,16 @@ private: check(code_ok, "test.c"); ASSERT_EQUALS("", errout.str()); } + + // don't crash + void trac5030() { + check("bool bob( char const **column_ptrs ) {\n" + "unique_ptrotherbuffer{new char[otherbufsize+1]};\n" + "char *const oldbuffer = otherbuffer.get();\n" + "int const oldbufsize = otherbufsize;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } };