MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / negativeArraySize

Method negativeArraySize

lib/checkbufferoverrun.cpp:1171–1199  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1169}
1170
1171void CheckBufferOverrunImpl::negativeArraySize()
1172{
1173 logChecker("CheckBufferOverrun::negativeArraySize");
1174 const SymbolDatabase* symbolDatabase = mTokenizer->getSymbolDatabase();
1175 for (const Variable* var : symbolDatabase->variableList()) {
1176 if (!var || !var->isArray())
1177 continue;
1178 const Token* const nameToken = var->nameToken();
1179 if (!Token::Match(nameToken, "%var% [") || !nameToken->next()->astOperand2())
1180 continue;
1181 const ValueFlow::Value* sz = nameToken->next()->astOperand2()->getValueLE(-1, mSettings);
1182 // don't warn about constant negative index because that is a compiler error
1183 if (sz && isVLAIndex(nameToken->next()->astOperand2()))
1184 negativeArraySizeError(nameToken);
1185 }
1186
1187 for (const Scope* functionScope : symbolDatabase->functionScopes) {
1188 for (const Token* tok = functionScope->bodyStart; tok != functionScope->bodyEnd; tok = tok->next()) {
1189 if (!tok->isKeyword() || tok->str() != "new" || !tok->astOperand1() || tok->astOperand1()->str() != "[")
1190 continue;
1191 const Token* valOperand = tok->astOperand1()->astOperand2();
1192 if (!valOperand)
1193 continue;
1194 const ValueFlow::Value* sz = valOperand->getValueLE(-1, mSettings);
1195 if (sz)
1196 negativeMemoryAllocationSizeError(tok, sz);
1197 }
1198 }
1199}
1200
1201void CheckBufferOverrunImpl::negativeArraySizeError(const Token* tok)
1202{

Callers 1

runChecksMethod · 0.45

Calls 9

isVLAIndexFunction · 0.85
isArrayMethod · 0.80
astOperand2Method · 0.80
nextMethod · 0.80
getValueLEMethod · 0.80
astOperand1Method · 0.80
nameTokenMethod · 0.45
isKeywordMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected