Null pointer: Fixed FP when there are 'return p?p->x:0;'

This commit is contained in:
Daniel Marjamäki 2012-12-25 14:23:50 +01:00
parent f16c30248a
commit f127d56719
2 changed files with 14 additions and 0 deletions

View File

@ -1013,6 +1013,8 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
nullPointerError(tok2, pointerName, vartok, inconclusive);
else if (unknown)
nullPointerError(tok2, pointerName, vartok, true);
if (Token::Match(tok2, "%var% ?"))
break;
}
}
break;

View File

@ -1679,6 +1679,18 @@ private:
" *x = 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
// return ?:
check("int f(ABC *p) {\n" // FP : return ?:
" if (!p) {}\n"
" return p ? p->x : 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int f(ABC *p) {\n" // no fn
" if (!p) {}\n"
" return q ? p->x : 0;\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
}
// Test CheckNullPointer::nullConstantDereference