From 8853f304affbb031c5199e5937dd4e226c011055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 8 Feb 2010 18:25:08 +0100 Subject: [PATCH] Fixed #1364 (False positive: (error) Possible null pointer dereference) --- lib/tokenize.cpp | 5 ++++- test/testsimplifytokens.cpp | 4 ++-- test/testtokenize.cpp | 7 ++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2516b1cd1..a2958a0fb 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4482,7 +4482,10 @@ bool Tokenizer::simplifyKnownVariables() break; // Using the variable in condition.. - if (Token::Match(tok3, "(|!|==|!=|<|<=|>|>= %varid% )|==|!=|<|<=|>|>=", varid)) + if (Token::Match(tok3->previous(), "if ( %varid% ==|!=|<|<=|>|>=|)", varid) || + Token::Match(tok3, "( %varid% ==|!=|<|<=|>|>=", varid) || + Token::Match(tok3, "!|==|!=|<|<=|>|>= %varid% ==|!=|<|<=|>|>=|)", varid) || + Token::Match(tok3->previous(), "strlen|free ( %varid% )", varid)) { tok3 = tok3->next(); tok3->str(value); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 048a758df..6cc5ef551 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -3268,14 +3268,14 @@ private: "{\n" " char buf[100];\n" " char *p = buf;\n" - " x(p);\n" + " free(p);\n" "}\n"; const char expected[] = "void f ( ) " "{ " "char buf [ 100 ] ; " "; ; " - "x ( buf ) ; " + "free ( buf ) ; " "}"; ASSERT_EQUALS(expected, tok(code)); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 3ac625337..2866bdaa9 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -786,8 +786,9 @@ private: " if ( g(a));\n" "}\n"; + // TODO: if a is passed by value is is ok to simplify.. ASSERT_EQUALS( - "void f ( ) { int a ; a = 4 ; if ( g ( 4 ) ) { ; } }", + "void f ( ) { int a ; a = 4 ; if ( g ( a ) ) { ; } }", simplifyKnownVariables(code)); } @@ -911,11 +912,11 @@ private: " {\n" " }\n" "\n" - " a(b);\n" + " return b;\n" "}\n"; ASSERT_EQUALS( - "void f ( ) { int b ; b = 0 ; b = 1 ; for ( int i = 0 ; i < 10 ; i ++ ) { } a ( 1 ) ; }", + "void f ( ) { int b ; b = 0 ; b = 1 ; for ( int i = 0 ; i < 10 ; i ++ ) { } return 1 ; }", simplifyKnownVariables(code)); } }