From ad1662a201362983962590db3bee5439499a50d9 Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Sun, 1 Jun 2014 22:18:17 +0200 Subject: [PATCH] #5875 fix 'lib/checksizeof.cpp:142:26: runtime error: member call on null pointer of type 'Token'. Improve error handling in CheckSizeof::checkSizeofForPointerSize() --- lib/checksizeof.cpp | 2 ++ test/testsizeof.cpp | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/checksizeof.cpp b/lib/checksizeof.cpp index c12f507b7..4325d35b5 100644 --- a/lib/checksizeof.cpp +++ b/lib/checksizeof.cpp @@ -139,6 +139,8 @@ void CheckSizeof::checkSizeofForPointerSize() } else if (Token::Match(tok, "memcpy|memcmp|memmove|strncpy|strncmp|strncat (")) { variable = tok->tokAt(2); variable2 = variable->nextArgument(); + if (!variable2) + continue; tokVar = variable2->nextArgument(); } else { diff --git a/test/testsizeof.cpp b/test/testsizeof.cpp index 7fddeee64..bca511067 100644 --- a/test/testsizeof.cpp +++ b/test/testsizeof.cpp @@ -40,6 +40,7 @@ private: TEST_CASE(sizeofForNumericParameter); TEST_CASE(suspiciousSizeofCalculation); TEST_CASE(sizeofVoid); + TEST_CASE(customStrncat); } void check(const char code[]) { @@ -635,6 +636,14 @@ private: ASSERT_EQUALS("[test.cpp:5]: (portability) '(foo[0]).data' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.\n", errout.str()); } + void customStrncat() { + // Ensure we don't crash on custom-defined strncat, ticket #5875 + check("char strncat ();\n" + "int main () {\n" + " return strncat ();\n" + "}\n"); + } + }; REGISTER_TEST(TestSizeof)