Skip to content

Commit cea6497

Browse files
Fix FN buffer overrun with array of pointers (#3582)
1 parent 1e327df commit cea6497

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

lib/checkbufferoverrun.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ static bool getDimensionsEtc(const Token * const arrayToken, const Settings *set
216216
Dimension dim;
217217
dim.known = value->isKnown();
218218
dim.tok = nullptr;
219-
const int typeSize = array->valueType()->typeSize(*settings);
219+
const int typeSize = array->valueType()->typeSize(*settings, array->valueType()->pointer > 1);
220220
if (typeSize == 0)
221221
return false;
222222
dim.num = value->intvalue / typeSize;

test/testbufferoverrun.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3449,6 +3449,20 @@ class TestBufferOverrun : public TestFixture {
34493449
" cache[i][0xFFFF] = 0;\n"
34503450
"}");
34513451
ASSERT_EQUALS("", errout.str());
3452+
3453+
check("void f() {\n"
3454+
" int **a = malloc(2 * sizeof(int*));\n"
3455+
" for (int i = 0; i < 3; i++)\n"
3456+
" a[i] = NULL;\n"
3457+
"}");
3458+
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Array 'a[2]' accessed at index 2, which is out of bounds.\n", errout.str());
3459+
3460+
check("void f() {\n"
3461+
" int **a = new int*[2];\n"
3462+
" for (int i = 0; i < 3; i++)\n"
3463+
" a[i] = NULL;\n"
3464+
"}");
3465+
TODO_ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Array 'a[2]' accessed at index 2, which is out of bounds.\n", "", errout.str());
34523466
}
34533467

34543468
// statically allocated buffer

0 commit comments

Comments
 (0)