Skip to content
Merged
7 changes: 5 additions & 2 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7078,8 +7078,11 @@ static void valueFlowDynamicBufferSize(const TokenList& tokenlist, const SymbolD
if (!typeTok || !typeTok->varId())
typeTok = newTok->astParent()->previous(); // hack for "int** z = ..."
if (typeTok && typeTok->valueType()) {
const MathLib::bigint typeSize = typeTok->valueType()->typeSize(settings.platform, typeTok->valueType()->pointer > 1);
if (typeSize >= 0)
ValueType vt = *typeTok->valueType();
if (vt.pointer > 0)
--vt.pointer;
const MathLib::bigint typeSize = ValueFlow::getSizeOf(vt, settings, ValueFlow::Accuracy::ExactOrZero);
if (typeSize > 0 || numElem == 0)
sizeValue = numElem * typeSize;
}
}
Expand Down
26 changes: 26 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7457,6 +7457,32 @@ class TestValueFlow : public TestFixture {
"}";
ASSERT_EQUALS(true, testValueOfX(code, 4U, 100, ValueFlow::Value::ValueType::BUFFER_SIZE));

code = "struct A {};\n" // #14305
"void* f() {\n"
" A* x = new A();\n"
" return x;\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 4U, 1, ValueFlow::Value::ValueType::BUFFER_SIZE));

code = "struct A {};\n"
"void* f() {\n"
" void* x = new A;\n"
" return x;\n"
"}";
{
auto values = tokenValues(code, "x ; }");
ASSERT_EQUALS(1, values.size());
ASSERT(values.front().isSymbolicValue());
// TODO: add BUFFER_SIZE value = 1
}

code = "struct B { int32_t i; };\n"
"void* f() {\n"
" B* x = new B();\n"
" return x;\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 4U, 4, ValueFlow::Value::ValueType::BUFFER_SIZE));

settings = settingsOld;
}

Expand Down
Loading