Skip to content

Commit aaa058f

Browse files
fixup! fixup! Fix #8260 Improve check: Pointer calculation result not null
1 parent 8304545 commit aaa058f

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

lib/checkcondition.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,10 @@ void CheckCondition::checkPointerAdditionResultNotNull()
18421842
if (tok->isExpandedMacro())
18431843
continue;
18441844

1845+
const bool usedAsBool = astIsPointer(tok) && isUsedAsBool(tok, *mSettings);
1846+
if (!tok->isComparisonOp() && !usedAsBool)
1847+
continue;
1848+
18451849
const Token *calcToken = getPointerAdditionCalcToken(tok);
18461850
if (!calcToken)
18471851
continue;
@@ -1856,8 +1860,8 @@ void CheckCondition::checkPointerAdditionResultNotNull()
18561860
continue;
18571861

18581862
pointerAdditionResultNotNullError(tok, calcToken);
1859-
} else if (astIsPointer(tok) && isUsedAsBool(tok, *mSettings) && !tok->astParent()->isComparisonOp()) {
1860-
pointerAdditionResultNotNullError(tok, calcToken);
1863+
} else if (usedAsBool && (!tok->astParent() || !tok->astParent()->isComparisonOp())) {
1864+
pointerArithmeticAlwaysTrueError(tok, calcToken);
18611865
}
18621866
}
18631867
}
@@ -1869,6 +1873,12 @@ void CheckCondition::pointerAdditionResultNotNullError(const Token *tok, const T
18691873
reportError(tok, Severity::warning, "pointerAdditionResultNotNull", "Comparison is wrong. Result of '" + s + "' can't be 0 unless there is pointer overflow, and pointer overflow is undefined behaviour.");
18701874
}
18711875

1876+
void CheckCondition::pointerArithmeticAlwaysTrueError(const Token *tok, const Token *calc)
1877+
{
1878+
const std::string s = calc ? calc->expressionString() : "ptr+1";
1879+
reportError(tok, Severity::warning, "pointerAdditionResultNotNull", "Pointer expression '" + s + "' is always true unless there is pointer overflow, and pointer overflow is undefined behaviour.");
1880+
}
1881+
18721882
void CheckCondition::checkDuplicateConditionalAssign()
18731883
{
18741884
if (!mSettings->severity.isEnabled(Severity::style) && !mSettings->isPremiumEnabled("duplicateConditionalAssign"))
@@ -2168,6 +2178,7 @@ void CheckCondition::getErrorMessages(ErrorLogger *errorLogger, const Settings *
21682178
c.alwaysTrueFalseError(nullptr, nullptr, nullptr);
21692179
c.invalidTestForOverflow(nullptr, nullptr, "false");
21702180
c.pointerAdditionResultNotNullError(nullptr, nullptr);
2181+
c.pointerArithmeticAlwaysTrueError(nullptr, nullptr);
21712182
c.duplicateConditionalAssignError(nullptr, nullptr);
21722183
c.assignmentInCondition(nullptr);
21732184
c.compareValueOutOfTypeRangeError(nullptr, "unsigned char", 256, true);

lib/checkcondition.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class CPPCHECKLIB CheckCondition : public Check {
150150

151151
void invalidTestForOverflow(const Token* tok, const ValueType *valueType, const std::string &replace);
152152
void pointerAdditionResultNotNullError(const Token *tok, const Token *calc);
153+
void pointerArithmeticAlwaysTrueError(const Token *tok, const Token *calc);
153154

154155
void duplicateConditionalAssignError(const Token *condTok, const Token* assignTok, bool isRedundant = false);
155156

test/testcondition.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6245,7 +6245,19 @@ class TestCondition : public TestFixture {
62456245
" int *q = ptr + 1;\n"
62466246
" if (q);\n"
62476247
"}");
6248-
ASSERT_EQUALS("[test.cpp:3:7]: (warning) Comparison is wrong. Result of 'q' can't be 0 unless there is pointer overflow, and pointer overflow is undefined behaviour. [pointerAdditionResultNotNull]\n", errout_str());
6248+
ASSERT_EQUALS("[test.cpp:3:7]: (warning) Pointer expression 'q' is always true unless there is pointer overflow, and pointer overflow is undefined behaviour. [pointerAdditionResultNotNull]\n", errout_str());
6249+
6250+
check("void f(char *ptr) {\n"
6251+
" int *q = ptr + 1;\n"
6252+
" if (!q);\n"
6253+
"}");
6254+
ASSERT_EQUALS("[test.cpp:3:8]: (warning) Pointer expression 'q' is always true unless there is pointer overflow, and pointer overflow is undefined behaviour. [pointerAdditionResultNotNull]\n", errout_str());
6255+
6256+
check("void f(char *ptr) {\n"
6257+
" int *q = ptr + 0;\n"
6258+
" if (q != 0);\n"
6259+
"}");
6260+
ASSERT_EQUALS("", errout_str());
62496261
}
62506262

62516263
void duplicateConditionalAssign() {

0 commit comments

Comments
 (0)