diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index c9fba00ce..1a0e4f593 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5319,11 +5319,10 @@ void Tokenizer::simplifyCasts() tok = tok->linkAt(2); continue; } - - while (Token::Match(tok->next(), "( %type% *| ) *|&| %var%") || - Token::Match(tok->next(), "( %type% %type% *| ) *|&| %var%") || - (!tok->isName() && (Token::Match(tok->next(), "( %type% * ) (") || - Token::Match(tok->next(), "( %type% %type% * ) (")))) { + while (Token::Match(tok->next(), "( %type% *| *| *| ) *|&| %var%") || + Token::Match(tok->next(), "( %type% %type% *| *| *| ) *|&| %var%") || + (!tok->isName() && (Token::Match(tok->next(), "( %type% * *| *| ) (") || + Token::Match(tok->next(), "( %type% %type% * *| *| ) (")))) { if (tok->isName() && tok->str() != "return") break; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 24ea2822f..bff1759c5 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -260,6 +260,7 @@ private: TEST_CASE(realloc12); TEST_CASE(realloc13); TEST_CASE(realloc14); + TEST_CASE(realloc15); TEST_CASE(assign1); TEST_CASE(assign2); // #2806 - FP when using redundant assignment @@ -2728,6 +2729,17 @@ private: ASSERT_EQUALS("", errout.str()); } + void realloc15() { + check("bool foo() {\n" + " char ** m_options;\n" + " m_options = (char**)realloc( m_options, 2 * sizeof(char*));\n" + " if( m_options == NULL )\n" + " return false;\n" + " return true;\n" + "}\n", false); + ASSERT_EQUALS("[test.cpp:3]: (error) Common realloc mistake: \'m_options\' nulled but not freed upon failure\n", errout.str()); + } + void assign1() { check("void foo()\n" "{\n" diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 0ba86f81b..1a38f8a7c 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -67,6 +67,8 @@ private: TEST_CASE(removeCast4); TEST_CASE(removeCast5); TEST_CASE(removeCast6); + TEST_CASE(removeCast7); + TEST_CASE(removeCast8); TEST_CASE(inlineasm); @@ -727,6 +729,14 @@ private: ASSERT_EQUALS("if ( ! x )", tokenizeAndStringify("if (x == (char *) ((void *)0))", true)); } + void removeCast7() { + ASSERT_EQUALS("str = malloc ( 3 )", tokenizeAndStringify("str=(char **)malloc(3)", true)); + } + + void removeCast8() { + ASSERT_EQUALS("ptr1 = ptr2", tokenizeAndStringify("ptr1=(int * **)ptr2", true)); + } + void inlineasm() { ASSERT_EQUALS("; asm ( ) ;", tokenizeAndStringify(";asm { mov ax,bx };")); ASSERT_EQUALS("; asm ( ) ;", tokenizeAndStringify(";_asm { mov ax,bx };"));