| 1169 | } |
| 1170 | |
| 1171 | void 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 | |
| 1201 | void CheckBufferOverrunImpl::negativeArraySizeError(const Token* tok) |
| 1202 | { |
no test coverage detected