Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions lib/checknullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,13 +509,27 @@ void CheckNullPointer::arithmetic()
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (!tok->astOperand2() || tok->str() != "-")
if (!Token::Match(tok, "-|+|+=|-=|++|--"))
continue;
// pointer subtraction
if (!tok->valueType() || !tok->valueType()->pointer)
const Token *pointerOperand;
if (tok->astOperand1() && tok->astOperand1()->valueType() && tok->astOperand1()->valueType()->pointer != 0)
pointerOperand = tok->astOperand1();
else if (tok->astOperand2() && tok->astOperand2()->valueType() && tok->astOperand2()->valueType()->pointer != 0)
pointerOperand = tok->astOperand2();
else
continue;
// Can LHS be NULL?
const ValueFlow::Value *value = tok->astOperand1()->getValue(0);
MathLib::bigint checkValue = 0;
// When using an assign op, the value read from
// valueflow has already been updated, so instead of
// checking for zero we check that the value is equal
// to RHS
if (tok->astOperand2() && tok->astOperand2()->hasKnownIntValue()) {
if (tok->str() == "-=")
checkValue -= tok->astOperand2()->values().front().intvalue;
else if (tok->str() == "+=")
checkValue = tok->astOperand2()->values().front().intvalue;
}
const ValueFlow::Value *value = pointerOperand->getValue(checkValue);
if (!value)
continue;
if (!_settings->inconclusive && value->isInconclusive())
Expand All @@ -530,10 +544,17 @@ void CheckNullPointer::arithmetic()
void CheckNullPointer::arithmeticError(const Token *tok, const ValueFlow::Value *value)
{
std::string errmsg;
if (value && value->condition)
errmsg = ValueFlow::eitherTheConditionIsRedundant(value->condition) + " or there is overflow in pointer subtraction.";
else
errmsg = "Overflow in pointer arithmetic, NULL pointer is subtracted.";
if (tok && tok->str().front() == '-') {
if (value && value->condition)
errmsg = ValueFlow::eitherTheConditionIsRedundant(value->condition) + " or there is overflow in pointer subtraction.";
else
errmsg = "Overflow in pointer arithmetic, NULL pointer is subtracted.";
} else {
if (value && value->condition)
errmsg = ValueFlow::eitherTheConditionIsRedundant(value->condition) + " or there is pointer arithmetic with NULL pointer.";
else
errmsg = "Pointer arithmetic with NULL pointer.";
}

std::list<const Token*> callstack;
callstack.push_back(tok);
Expand Down
12 changes: 12 additions & 0 deletions lib/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ inline static int caseInsensitiveStringCompare(const std::string &lhs, const std
return 0;
}

template< class T, std::size_t N >
T* begin( T (&array)[N] )
{
return array;
}

template< class T, std::size_t N >
T* end( T (&array)[N] )
{
return array+N;
}

#define UNUSED(x) (void)(x)

#endif
63 changes: 63 additions & 0 deletions test/testnullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class TestNullPointer : public TestFixture {
TEST_CASE(nullpointer_internal_error); // #5080
TEST_CASE(ticket6505);
TEST_CASE(subtract);
TEST_CASE(addNull);
}

void check(const char code[], bool inconclusive = false, const char filename[] = "test.cpp") {
Expand Down Expand Up @@ -2571,6 +2572,68 @@ class TestNullPointer : public TestFixture {
" p = s - 20;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition '!s' is redundant or there is overflow in pointer subtraction.\n", errout.str());

check("void foo(char *s) {\n"
" s -= 20;\n"
"}\n"
"void bar() { foo(0); }\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Overflow in pointer arithmetic, NULL pointer is subtracted.\n", errout.str());

check("void foo(char *s) {\n"
" if (!s) {}\n"
" s -= 20;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition '!s' is redundant or there is overflow in pointer subtraction.\n", errout.str());

check("int* f8() { int *x = NULL; return --x; }");
ASSERT_EQUALS("[test.cpp:1]: (error) Overflow in pointer arithmetic, NULL pointer is subtracted.\n", errout.str());

check("int* f9() { int *x = NULL; return x--; }");
ASSERT_EQUALS("[test.cpp:1]: (error) Overflow in pointer arithmetic, NULL pointer is subtracted.\n", errout.str());
}

void addNull() {
check("void foo(char *s) {\n"
" char * p = s + 20;\n"
"}\n"
"void bar() { foo(0); }\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Pointer arithmetic with NULL pointer.\n", errout.str());

check("void foo(char *s) {\n"
" if (!s) {}\n"
" char * p = s + 20;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition '!s' is redundant or there is pointer arithmetic with NULL pointer.\n", errout.str());

check("void foo(char *s) {\n"
" char * p = 20 + s;\n"
"}\n"
"void bar() { foo(0); }\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Pointer arithmetic with NULL pointer.\n", errout.str());

check("void foo(char *s) {\n"
" if (!s) {}\n"
" char * p = 20 + s;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition '!s' is redundant or there is pointer arithmetic with NULL pointer.\n", errout.str());

check("void foo(char *s) {\n"
" s += 20;\n"
"}\n"
"void bar() { foo(0); }\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Pointer arithmetic with NULL pointer.\n", errout.str());

check("void foo(char *s) {\n"
" if (!s) {}\n"
" s += 20;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition '!s' is redundant or there is pointer arithmetic with NULL pointer.\n", errout.str());

check("int* f7() { int *x = NULL; return ++x; }");
ASSERT_EQUALS("[test.cpp:1]: (error) Pointer arithmetic with NULL pointer.\n", errout.str());

check("int* f10() { int *x = NULL; return x++; } ");
ASSERT_EQUALS("[test.cpp:1]: (error) Pointer arithmetic with NULL pointer.\n", errout.str());
}
};

Expand Down