Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7025,6 +7025,12 @@ static void valueFlowDynamicBufferSize(const TokenList& tokenlist, const SymbolD
auto getBufferSizeFromNew = [&](const Token* newTok) -> MathLib::bigint {
MathLib::bigint sizeValue = -1, numElem = -1;

// ::operator new(size_t size)
if (Token::Match(newTok->astOperand1(), "::| operatornew")) {
const Token *sizeTok = newTok->astOperand2();
return sizeTok->hasKnownIntValue() ? sizeTok->getKnownIntValue() : -1;
}

if (newTok && newTok->astOperand1()) { // number of elements
const Token* bracTok = nullptr, *typeTok = nullptr;
if (newTok->astOperand1()->str() == "[")
Expand Down Expand Up @@ -7071,7 +7077,7 @@ static void valueFlowDynamicBufferSize(const TokenList& tokenlist, const SymbolD
if (!rhs)
continue;

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

Expand Down
7 changes: 7 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7932,6 +7932,13 @@ class TestValueFlow : public TestFixture {
" return x;\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 4U, 4, ValueFlow::Value::ValueType::BUFFER_SIZE, &settingsCfg));

code = "void f()\n"
"{\n"
" void *x = ::operator new(0);\n"
" (void) x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfX(code, 4U, 0, ValueFlow::Value::ValueType::BUFFER_SIZE));
}

void valueFlowSafeFunctionParameterValues() {
Expand Down
Loading