Skip to content

Commit ab9989c

Browse files
authored
Fix #14978: Valueflow: Missing buffer size for ::operator new (#8799)
1 parent 7d48e00 commit ab9989c

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

lib/valueflow.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7025,6 +7025,12 @@ static void valueFlowDynamicBufferSize(const TokenList& tokenlist, const SymbolD
70257025
auto getBufferSizeFromNew = [&](const Token* newTok) -> MathLib::bigint {
70267026
MathLib::bigint sizeValue = -1, numElem = -1;
70277027

7028+
// ::operator new(size_t size)
7029+
if (Token::Match(newTok->astOperand1(), "::| operatornew")) {
7030+
const Token *sizeTok = newTok->astOperand2();
7031+
return sizeTok->hasKnownIntValue() ? sizeTok->getKnownIntValue() : -1;
7032+
}
7033+
70287034
if (newTok && newTok->astOperand1()) { // number of elements
70297035
const Token* bracTok = nullptr, *typeTok = nullptr;
70307036
if (newTok->astOperand1()->str() == "[")
@@ -7071,7 +7077,7 @@ static void valueFlowDynamicBufferSize(const TokenList& tokenlist, const SymbolD
70717077
if (!rhs)
70727078
continue;
70737079

7074-
const bool isNew = rhs->isCpp() && rhs->str() == "new";
7080+
const bool isNew = rhs->isCpp() && (rhs->str() == "new" || Token::Match(rhs->astOperand1(), "::| operatornew"));
70757081
if (!isNew && !Token::Match(rhs->previous(), "%name% ("))
70767082
continue;
70777083

test/testvalueflow.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7932,6 +7932,13 @@ class TestValueFlow : public TestFixture {
79327932
" return x;\n"
79337933
"}";
79347934
ASSERT_EQUALS(true, testValueOfX(code, 4U, 4, ValueFlow::Value::ValueType::BUFFER_SIZE, &settingsCfg));
7935+
7936+
code = "void f()\n"
7937+
"{\n"
7938+
" void *x = ::operator new(0);\n"
7939+
" (void) x;\n"
7940+
"}\n";
7941+
ASSERT_EQUALS(true, testValueOfX(code, 4U, 0, ValueFlow::Value::ValueType::BUFFER_SIZE));
79357942
}
79367943

79377944
void valueFlowSafeFunctionParameterValues() {

0 commit comments

Comments
 (0)