Skip to content

Commit 93ac5a4

Browse files
committed
Fixed cppcheck-opensource#6346 (pointer calculation overflow)
1 parent 293dc1e commit 93ac5a4

2 files changed

Lines changed: 41 additions & 8 deletions

File tree

lib/checkbufferoverrun.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void CheckBufferOverrun::outOfBoundsError(const Token *tok, const std::string &w
175175

176176
void CheckBufferOverrun::pointerOutOfBoundsError(const Token *tok, const std::string &object)
177177
{
178-
reportError(tok, Severity::portability, "pointerOutOfBounds", "Undefined behaviour: Pointer arithmetic result does not point into or just past the end of the " + object + ".\n"
178+
reportError(tok, Severity::error, "pointerOutOfBounds", "Undefined behaviour: Pointer arithmetic result does not point into or just past the end of the " + object + ".\n"
179179
"Undefined behaviour: The result of this pointer arithmetic does not point into or just one element past the end of the " + object + ". Further information: https://www.securecoding.cert.org/confluence/display/seccode/ARR30-C.+Do+not+form+or+use+out+of+bounds+pointers+or+array+subscripts");
180180
}
181181

@@ -829,7 +829,6 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
829829

830830
const unsigned int declarationId = arrayInfo.declarationId();
831831

832-
const bool isPortabilityEnabled = _settings->isEnabled("portability");
833832
const bool isWarningEnabled = _settings->isEnabled("warning");
834833

835834
for (const Token* const end = tok->scope()->classEnd; tok != end; tok = tok->next()) {
@@ -838,13 +837,32 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
838837
valueFlowCheckArrayIndex(tok->next(), arrayInfo);
839838
}
840839

841-
// undefined behaviour: result of pointer arithmetic is out of bounds
842-
else if (isPortabilityEnabled && Token::Match(tok->previous(), "= %varid% + %num% ;", declarationId)) {
843-
const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(2));
844-
if (index < 0 || index > arrayInfo.num(0)) {
840+
else if (tok->astParent() && tok->astParent()->str() == "+") {
841+
const ValueFlow::Value *index;
842+
if (tok == tok->astParent()->astOperand1())
843+
index = tok->astParent()->astOperand2()->getMaxValue(false);
844+
else
845+
index = tok->astParent()->astOperand1()->getMaxValue(false);
846+
847+
// undefined behaviour: result of pointer arithmetic is out of bounds
848+
if (index && (index->intvalue < 0 || index->intvalue > arrayInfo.num(0))) {
845849
pointerOutOfBoundsError(tok, "array");
846850
}
847851
}
852+
853+
else if (tok->astParent() && tok->astParent()->str() == "-") {
854+
const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(declarationId);
855+
if (var && var->isArray()) {
856+
const Token *index;
857+
if (tok == tok->astParent()->astOperand1())
858+
index = tok->astParent()->astOperand2();
859+
else
860+
index = tok->astParent()->astOperand1();
861+
862+
if (index && index->getValueGE(1,_settings))
863+
pointerOutOfBoundsError(tok, "array");
864+
}
865+
}
848866
}
849867

850868
else if (!tok->scope()->isExecutable()) // No executable code outside of executable scope - continue to increase performance

test/testbufferoverrun.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ class TestBufferOverrun : public TestFixture {
232232
// char *p2 = a + 11 // UB
233233
TEST_CASE(pointer_out_of_bounds_1);
234234
TEST_CASE(pointer_out_of_bounds_2);
235+
TEST_CASE(pointer_out_of_bounds_sub);
235236

236237
TEST_CASE(sprintf1);
237238
TEST_CASE(sprintf2);
@@ -2947,7 +2948,13 @@ class TestBufferOverrun : public TestFixture {
29472948
" char a[10];\n"
29482949
" char *p = a + 100;\n"
29492950
"}");
2950-
ASSERT_EQUALS("[test.cpp:3]: (portability) Undefined behaviour: Pointer arithmetic result does not point into or just past the end of the array.\n", errout.str());
2951+
ASSERT_EQUALS("[test.cpp:3]: (error) Undefined behaviour: Pointer arithmetic result does not point into or just past the end of the array.\n", errout.str());
2952+
2953+
check("void f() {\n"
2954+
" char a[10];\n"
2955+
" return a + 100;\n"
2956+
"}");
2957+
ASSERT_EQUALS("[test.cpp:3]: (error) Undefined behaviour: Pointer arithmetic result does not point into or just past the end of the array.\n", errout.str());
29512958
}
29522959

29532960
void pointer_out_of_bounds_2() {
@@ -2956,7 +2963,7 @@ class TestBufferOverrun : public TestFixture {
29562963
" p += 100;\n"
29572964
" free(p);"
29582965
"}");
2959-
ASSERT_EQUALS("[test.cpp:3]: (portability) Undefined behaviour: Pointer arithmetic result does not point into or just past the end of the buffer.\n", errout.str());
2966+
ASSERT_EQUALS("[test.cpp:3]: (error) Undefined behaviour: Pointer arithmetic result does not point into or just past the end of the buffer.\n", errout.str());
29602967

29612968
check("void f() {\n"
29622969
" char *p = malloc(10);\n"
@@ -2985,6 +2992,14 @@ class TestBufferOverrun : public TestFixture {
29852992
ASSERT_EQUALS("", errout.str());
29862993
}
29872994

2995+
void pointer_out_of_bounds_sub() {
2996+
check("void f() {\n"
2997+
" char x[10];\n"
2998+
" return x-1;\n"
2999+
"}");
3000+
ASSERT_EQUALS("[test.cpp:3]: (error) Undefined behaviour: Pointer arithmetic result does not point into or just past the end of the array.\n", errout.str());
3001+
}
3002+
29883003
void sprintf1() {
29893004
check("void f()\n"
29903005
"{\n"

0 commit comments

Comments
 (0)