Fixed #4203 (Don't warn about setting NULL value for pointers and not using that value)

This commit is contained in:
Daniel Marjamäki 2012-09-30 17:22:35 +02:00
parent 9b093b404c
commit a64669b1ec
3 changed files with 13 additions and 2 deletions

View File

@ -5,4 +5,4 @@ compiler:
script: script:
- make test - make test
- $CXX -o cppcheck -O2 cli/*.cpp lib/*.cpp -Ilib - $CXX -o cppcheck -O2 cli/*.cpp lib/*.cpp -Ilib
- ./cppcheck --error-exitcode=1 -Ilib --enable=style --suppress=unreadVariable --suppress=duplicateBranch --suppress=stlIfStrFind -q cli gui lib -igui/test - ./cppcheck --error-exitcode=1 -Ilib --enable=style --suppress=duplicateBranch --suppress=stlIfStrFind -q cli gui lib -igui/test

View File

@ -250,7 +250,7 @@ void Variables::write(unsigned int varid, const Token* tok)
if (usage) { if (usage) {
usage->_write = true; usage->_write = true;
if (!usage->_var->isStatic()) if (!usage->_var->isStatic() && !Token::simpleMatch(tok->next(), "= 0 ;"))
usage->_read = false; usage->_read = false;
usage->_lastAccess = tok; usage->_lastAccess = tok;
} }

View File

@ -144,6 +144,7 @@ private:
TEST_CASE(localvarIfNOT); // #3104 - if ( NOT var ) TEST_CASE(localvarIfNOT); // #3104 - if ( NOT var )
TEST_CASE(localvarAnd); // #3672 TEST_CASE(localvarAnd); // #3672
TEST_CASE(localvarSwitch); // #3744 - false positive when localvar is used in switch TEST_CASE(localvarSwitch); // #3744 - false positive when localvar is used in switch
TEST_CASE(localvarNULL); // #4203 - Setting NULL value is not redundant - it is safe
TEST_CASE(crash1); TEST_CASE(crash1);
} }
@ -3317,6 +3318,16 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void localvarNULL() { // #4203 - Setting NULL value is not redundant - it is safe
functionVariableUsage("void f() {\n"
" char *p = malloc(100);\n"
" foo(p);\n"
" free(p);\n"
" p = NULL;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void crash1() { void crash1() {
functionVariableUsage("SAL_WNODEPRECATED_DECLARATIONS_PUSH\n" functionVariableUsage("SAL_WNODEPRECATED_DECLARATIONS_PUSH\n"
"void convertToTokenArray() {\n" "void convertToTokenArray() {\n"