Skip to content

Commit 5c247e7

Browse files
fixup! fixup! Fix #8260 Improve check: Pointer calculation result not null
1 parent ec4f0cc commit 5c247e7

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
@@ -1847,6 +1847,10 @@ void CheckConditionImpl::checkPointerAdditionResultNotNull()
18471847
if (tok->isExpandedMacro())
18481848
continue;
18491849

1850+
const bool usedAsBool = astIsPointer(tok) && isUsedAsBool(tok, *mSettings);
1851+
if (!tok->isComparisonOp() && !usedAsBool)
1852+
continue;
1853+
18501854
const Token *calcToken = getPointerAdditionCalcToken(tok);
18511855
if (!calcToken)
18521856
continue;
@@ -1861,8 +1865,8 @@ void CheckConditionImpl::checkPointerAdditionResultNotNull()
18611865
continue;
18621866

18631867
pointerAdditionResultNotNullError(tok, calcToken);
1864-
} else if (astIsPointer(tok) && isUsedAsBool(tok, *mSettings) && !tok->astParent()->isComparisonOp()) {
1865-
pointerAdditionResultNotNullError(tok, calcToken);
1868+
} else if (usedAsBool && (!tok->astParent() || !tok->astParent()->isComparisonOp())) {
1869+
pointerArithmeticAlwaysTrueError(tok, calcToken);
18661870
}
18671871
}
18681872
}
@@ -1874,6 +1878,12 @@ void CheckConditionImpl::pointerAdditionResultNotNullError(const Token *tok, con
18741878
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.");
18751879
}
18761880

1881+
void CheckConditionImpl::pointerArithmeticAlwaysTrueError(const Token *tok, const Token *calc)
1882+
{
1883+
const std::string s = calc ? calc->expressionString() : "ptr+1";
1884+
reportError(tok, Severity::warning, "pointerAdditionResultNotNull", "Pointer expression '" + s + "' is always true unless there is pointer overflow, and pointer overflow is undefined behaviour.");
1885+
}
1886+
18771887
void CheckConditionImpl::checkDuplicateConditionalAssign()
18781888
{
18791889
if (!mSettings->severity.isEnabled(Severity::style) && !mSettings->isPremiumEnabled("duplicateConditionalAssign"))
@@ -2173,6 +2183,7 @@ void CheckCondition::getErrorMessages(ErrorLogger *errorLogger, const Settings *
21732183
c.alwaysTrueFalseError(nullptr, nullptr, nullptr);
21742184
c.invalidTestForOverflow(nullptr, nullptr, "false");
21752185
c.pointerAdditionResultNotNullError(nullptr, nullptr);
2186+
c.pointerArithmeticAlwaysTrueError(nullptr, nullptr);
21762187
c.duplicateConditionalAssignError(nullptr, nullptr);
21772188
c.assignmentInCondition(nullptr);
21782189
c.compareValueOutOfTypeRangeError(nullptr, "unsigned char", 256, true);

lib/checkcondition.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ class CPPCHECKLIB CheckConditionImpl : public CheckImpl {
175175

176176
void invalidTestForOverflow(const Token* tok, const ValueType *valueType, const std::string &replace);
177177
void pointerAdditionResultNotNullError(const Token *tok, const Token *calc);
178+
void pointerArithmeticAlwaysTrueError(const Token *tok, const Token *calc);
178179

179180
void duplicateConditionalAssignError(const Token *condTok, const Token* assignTok, bool isRedundant = false);
180181

test/testcondition.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6259,7 +6259,19 @@ class TestCondition : public TestFixture {
62596259
" int *q = ptr + 1;\n"
62606260
" if (q);\n"
62616261
"}");
6262-
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());
6262+
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());
6263+
6264+
check("void f(char *ptr) {\n"
6265+
" int *q = ptr + 1;\n"
6266+
" if (!q);\n"
6267+
"}");
6268+
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());
6269+
6270+
check("void f(char *ptr) {\n"
6271+
" int *q = ptr + 0;\n"
6272+
" if (q != 0);\n"
6273+
"}");
6274+
ASSERT_EQUALS("", errout_str());
62636275
}
62646276

62656277
void duplicateConditionalAssign() {

0 commit comments

Comments
 (0)