Fix #745 (false positive: possible null pointer dereference , while)

http://sourceforge.net/apps/trac/cppcheck/ticket/745
This commit is contained in:
Reijo Tomperi 2009-09-30 21:42:14 +03:00
parent ec44f8f6c7
commit f9871c2d54
5 changed files with 30 additions and 23 deletions

View File

@ -464,6 +464,9 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[])
return false; return false;
} }
simplifyDoWhileAddBraces();
simplifyIfAddBraces();
// Combine "- %num%" .. // Combine "- %num%" ..
for (Token *tok = _tokens; tok; tok = tok->next()) for (Token *tok = _tokens; tok; tok = tok->next())
{ {
@ -1602,6 +1605,12 @@ void Tokenizer::simplifyTokenList()
simplifyGoto(); simplifyGoto();
// TODO, simplifyDoWhileAddBraces and simplifyIfAddBraces calls
// should be removed from here, but currently simplifyGoto
// removes braces which causes some tests to fail.
simplifyDoWhileAddBraces();
simplifyIfAddBraces();
// Combine wide strings // Combine wide strings
for (Token *tok = _tokens; tok; tok = tok->next()) for (Token *tok = _tokens; tok; tok = tok->next())
{ {
@ -1809,8 +1818,6 @@ void Tokenizer::simplifyTokenList()
} }
} }
simplifyDoWhileAddBraces();
simplifyIfAddBraces();
simplifyFunctionParameters(); simplifyFunctionParameters();
elseif(); elseif();

View File

@ -389,8 +389,8 @@ private:
ASSERT_EQUALS(";;if{exit;}", getcode("char *s; if (a) { exit(0); }", "s")); ASSERT_EQUALS(";;if{exit;}", getcode("char *s; if (a) { exit(0); }", "s"));
// open/close // open/close
ASSERT_EQUALS(";;alloc;if(var)dealloc;", getcode("int f; f=open(); if(f>=0)close(f);", "f")); ASSERT_EQUALS(";;alloc;if(var){dealloc;}", getcode("int f; f=open(); if(f>=0)close(f);", "f"));
ASSERT_EQUALS(";;alloc;ifv;", getcode("int f; f=open(); if(f!=-1 || x);", "f")); ASSERT_EQUALS(";;alloc;ifv{;}", getcode("int f; f=open(); if(f!=-1 || x);", "f"));
} }

View File

@ -816,7 +816,7 @@ private:
" else\n" " else\n"
" p = p->next();\n" " p = p->next();\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }

View File

@ -130,10 +130,10 @@ private:
std::istringstream istr(code); std::istringstream istr(code);
Tokenizer tokenizer; Tokenizer tokenizer;
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
if (simplify) if (simplify)
tokenizer.simplifyTokenList(); tokenizer.simplifyTokenList();
tokenizer.validate();
std::string ret; std::string ret;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
{ {
@ -1169,10 +1169,10 @@ private:
void ifassign1() void ifassign1()
{ {
ASSERT_EQUALS("; a = b ; if ( a ) ;", simplifyIfAssign(";if(a=b);")); ASSERT_EQUALS("; a = b ; if ( a ) { ; }", simplifyIfAssign(";if(a=b);"));
ASSERT_EQUALS("; a = b ( ) ; if ( ( a ) ) ;", simplifyIfAssign(";if((a=b()));")); ASSERT_EQUALS("; a = b ( ) ; if ( ( a ) ) { ; }", simplifyIfAssign(";if((a=b()));"));
ASSERT_EQUALS("; a = b ( ) ; if ( ! ( a ) ) ;", simplifyIfAssign(";if(!(a=b()));")); ASSERT_EQUALS("; a = b ( ) ; if ( ! ( a ) ) { ; }", simplifyIfAssign(";if(!(a=b()));"));
ASSERT_EQUALS("; a . x = b ( ) ; if ( ! ( a . x ) ) ;", simplifyIfAssign(";if(!(a->x=b()));")); ASSERT_EQUALS("; a . x = b ( ) ; if ( ! ( a . x ) ) { ; }", simplifyIfAssign(";if(!(a->x=b()));"));
} }
@ -1241,16 +1241,16 @@ private:
void and1() void and1()
{ {
ASSERT_EQUALS("if ( p && q ) ;", ASSERT_EQUALS("if ( p && q ) { ; }",
simplifyLogicalOperators("if (p and q) ;")); simplifyLogicalOperators("if (p and q) ;"));
ASSERT_EQUALS("if ( foo ( ) && q ) ;", ASSERT_EQUALS("if ( foo ( ) && q ) { ; }",
simplifyLogicalOperators("if (foo() and q) ;")); simplifyLogicalOperators("if (foo() and q) ;"));
ASSERT_EQUALS("if ( foo ( ) && bar ( ) ) ;", ASSERT_EQUALS("if ( foo ( ) && bar ( ) ) { ; }",
simplifyLogicalOperators("if (foo() and bar()) ;")); simplifyLogicalOperators("if (foo() and bar()) ;"));
ASSERT_EQUALS("if ( p && bar ( ) ) ;", ASSERT_EQUALS("if ( p && bar ( ) ) { ; }",
simplifyLogicalOperators("if (p and bar()) ;")); simplifyLogicalOperators("if (p and bar()) ;"));
} }

View File

@ -578,7 +578,7 @@ private:
"}\n"; "}\n";
ASSERT_EQUALS( ASSERT_EQUALS(
"void f ( ) { int a ; a = 10 ; if ( 10 ) ; }", "void f ( ) { int a ; a = 10 ; if ( 10 ) { ; } }",
simplifyKnownVariables(code)); simplifyKnownVariables(code));
} }
@ -590,7 +590,7 @@ private:
"}\n"; "}\n";
ASSERT_EQUALS( ASSERT_EQUALS(
"void f ( ) { int a ; a = 10 ; if ( ! 10 ) ; }", "void f ( ) { int a ; a = 10 ; if ( ! 10 ) { ; } }",
simplifyKnownVariables(code)); simplifyKnownVariables(code));
} }
} }
@ -605,7 +605,7 @@ private:
"}\n"; "}\n";
ASSERT_EQUALS( ASSERT_EQUALS(
"void f ( ) { int a ; a = 10 ; a = g ( ) ; if ( a ) ; }", "void f ( ) { int a ; a = 10 ; a = g ( ) ; if ( a ) { ; } }",
simplifyKnownVariables(code)); simplifyKnownVariables(code));
} }
@ -622,7 +622,7 @@ private:
"}\n"; "}\n";
ASSERT_EQUALS( ASSERT_EQUALS(
"void f ( ) { int a ; a = 4 ; while ( true ) { break ; a = 10 ; } if ( a ) ; }", "void f ( ) { int a ; a = 4 ; while ( true ) { break ; a = 10 ; } if ( a ) { ; } }",
simplifyKnownVariables(code)); simplifyKnownVariables(code));
} }
@ -635,7 +635,7 @@ private:
"}\n"; "}\n";
ASSERT_EQUALS( ASSERT_EQUALS(
"void f ( ) { int a ; a = 4 ; if ( g ( 4 ) ) ; }", "void f ( ) { int a ; a = 4 ; if ( g ( 4 ) ) { ; } }",
simplifyKnownVariables(code)); simplifyKnownVariables(code));
} }
@ -648,7 +648,7 @@ private:
"}\n"; "}\n";
ASSERT_EQUALS( ASSERT_EQUALS(
"void f ( ) { int a ; a = 4 ; if ( a = 5 ) ; }", "void f ( ) { int a ; a = 4 ; if ( a = 5 ) { ; } }",
simplifyKnownVariables(code)); simplifyKnownVariables(code));
} }
@ -704,7 +704,7 @@ private:
"}\n"; "}\n";
ASSERT_EQUALS( ASSERT_EQUALS(
"void foo ( ) { int a ; a = 1 ; int b ; b = 2 ; if ( 1 < 2 ) ; }", "void foo ( ) { int a ; a = 1 ; int b ; b = 2 ; if ( 1 < 2 ) { ; } }",
simplifyKnownVariables(code)); simplifyKnownVariables(code));
} }
@ -953,8 +953,8 @@ private:
"2: void f ( )\n" "2: void f ( )\n"
"3: {\n" "3: {\n"
"4: int i@2 ; i@2 = 2 ;\n" "4: int i@2 ; i@2 = 2 ;\n"
"5: for ( int i@3 = 0 ; i@3 < 10 ; ++ i@3 )\n" "5: for ( int i@3 = 0 ; i@3 < 10 ; ++ i@3 ) {\n"
"6: i@3 = 3 ;\n" "6: i@3 = 3 ; }\n"
"7: i@2 = 4 ;\n" "7: i@2 = 4 ;\n"
"8: }\n"); "8: }\n");