Skip to content

Commit de86e71

Browse files
committed
Fix #14305 Wrong buffer sizes computed by valueFlowDynamicBufferSize()
1 parent 602da94 commit de86e71

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

lib/valueflow.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7078,8 +7078,11 @@ static void valueFlowDynamicBufferSize(const TokenList& tokenlist, const SymbolD
70787078
if (!typeTok || !typeTok->varId())
70797079
typeTok = newTok->astParent()->previous(); // hack for "int** z = ..."
70807080
if (typeTok && typeTok->valueType()) {
7081-
const MathLib::bigint typeSize = typeTok->valueType()->typeSize(settings.platform, typeTok->valueType()->pointer > 1);
7082-
if (typeSize >= 0)
7081+
ValueType vt = *typeTok->valueType();
7082+
if (vt.pointer > 0)
7083+
--vt.pointer;
7084+
const MathLib::bigint typeSize = ValueFlow::getSizeOf(vt, settings, ValueFlow::Accuracy::ExactOrZero);
7085+
if (typeSize > 0 || numElem == 0)
70837086
sizeValue = numElem * typeSize;
70847087
}
70857088
}

test/testvalueflow.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7457,6 +7457,32 @@ class TestValueFlow : public TestFixture {
74577457
"}";
74587458
ASSERT_EQUALS(true, testValueOfX(code, 4U, 100, ValueFlow::Value::ValueType::BUFFER_SIZE));
74597459

7460+
code = "struct A {};\n" // #14305
7461+
"void* f() {\n"
7462+
" A* x = new A();\n"
7463+
" return x;\n"
7464+
"}";
7465+
ASSERT_EQUALS(true, testValueOfX(code, 4U, 1, ValueFlow::Value::ValueType::BUFFER_SIZE));
7466+
7467+
code = "struct A {};\n"
7468+
"void* f() {\n"
7469+
" void* x = new A;\n"
7470+
" return x;\n"
7471+
"}";
7472+
{
7473+
auto values = tokenValues(code, "x ; }");
7474+
ASSERT_EQUALS(1, values.size());
7475+
ASSERT(values.front().isSymbolicValue());
7476+
// TODO: add BUFFER_SIZE value = 1
7477+
}
7478+
7479+
code = "struct B { int32_t i; };\n"
7480+
"void* f() {\n"
7481+
" B* x = new B();\n"
7482+
" return x;\n"
7483+
"}";
7484+
ASSERT_EQUALS(true, testValueOfX(code, 4U, 4, ValueFlow::Value::ValueType::BUFFER_SIZE));
7485+
74607486
settings = settingsOld;
74617487
}
74627488

0 commit comments

Comments
 (0)