File tree Expand file tree Collapse file tree 2 files changed +42
-7
lines changed
Expand file tree Collapse file tree 2 files changed +42
-7
lines changed Original file line number Diff line number Diff line change @@ -446,14 +446,19 @@ void CheckNullPointer::nullConstantDereference()
446446 }
447447
448448 const Variable *ovar = nullptr ;
449- if (Token::Match (tok, " 0 ==|!=|>|>=|<|<= %var% !!." ))
450- ovar = tok->tokAt (2 )->variable ();
451- else if (Token::Match (tok, " %var% ==|!=|>|>=|<|<= 0" ))
452- ovar = tok->variable ();
453- else if (Token::Match (tok, " %var% =|+ 0 )|]|,|;|+" ))
449+ const Token *tokNull = nullptr ;
450+ if (Token::Match (tok, " 0 ==|!=|>|>=|<|<= %var%" )) {
451+ if (!Token::Match (tok->tokAt (3 )," .|[" )) {
452+ ovar = tok->tokAt (2 )->variable ();
453+ tokNull = tok;
454+ }
455+ } else if (Token::Match (tok, " %var% ==|!=|>|>=|<|<= 0" ) ||
456+ Token::Match (tok, " %var% =|+ 0 )|]|,|;|+" )) {
454457 ovar = tok->variable ();
455- if (ovar && !ovar->isPointer () && !ovar->isArray () && ovar->isStlStringType () && tok->tokAt (2 )->originalName () != " '\\ 0'" )
456- nullPointerError (tok);
458+ tokNull = tok->tokAt (2 );
459+ }
460+ if (ovar && !ovar->isPointer () && !ovar->isArray () && ovar->isStlStringType () && tokNull && tokNull->originalName () != " '\\ 0'" )
461+ nullPointerError (tokNull);
457462 }
458463 }
459464}
Original file line number Diff line number Diff line change @@ -2030,6 +2030,36 @@ class TestNullPointer : public TestFixture {
20302030 " [test.cpp:7]: (error) Possible null pointer dereference: p\n "
20312031 " [test.cpp:8]: (error) Possible null pointer dereference: p\n " , errout.str ());
20322032
2033+ check (" void f(std::string s1, const std::string& s2, const std::string* s3) {\n "
2034+ " void* p = 0;\n "
2035+ " if (x) { return; }\n "
2036+ " foo(0 == s1.size());\n "
2037+ " foo(0 == s2.size());\n "
2038+ " foo(0 == s3->size());\n "
2039+ " foo(s1.size() == 0);\n "
2040+ " foo(s2.size() == 0);\n "
2041+ " foo(s3->size() == 0);\n "
2042+ " }" , true );
2043+ ASSERT_EQUALS (" " , errout.str ());
2044+
2045+ check (" void f(std::string s1, const std::string& s2) {\n "
2046+ " if (x) { return; }\n "
2047+ " foo(0 == s1[0]);\n "
2048+ " foo(0 == s2[0]);\n "
2049+ " foo(s1[0] == 0);\n "
2050+ " foo(s2[0] == 0);\n "
2051+ " }" , true );
2052+ ASSERT_EQUALS (" " , errout.str ());
2053+
2054+ check (" void f(std::string s1, const std::string& s2) {\n "
2055+ " if (x) { return; }\n "
2056+ " foo(s1 == '\\ 0');\n "
2057+ " foo(s2 == '\\ 0');\n "
2058+ " foo('\\ 0' == s1);\n "
2059+ " foo('\\ 0' == s2);\n "
2060+ " }" , true );
2061+ ASSERT_EQUALS (" " , errout.str ());
2062+
20332063 check (" class Bar {\n "
20342064 " std::string s;\n "
20352065 " Bar() : s(0) {}\n "
You can’t perform that action at this time.
0 commit comments