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

Function getDimensionsEtc

lib/checkbufferoverrun.cpp:193–234  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

191//---------------------------------------------------------------------------
192
193static bool getDimensionsEtc(const Token * const arrayToken, const Settings &settings, std::vector<Dimension> &dimensions, ErrorPath &errorPath, bool &mightBeLarger, MathLib::bigint &path)
194{
195 const Token *array = arrayToken;
196 while (Token::Match(array, ".|::"))
197 array = array->astOperand2();
198
199 if (array->variable() && array->variable()->isArray() && !array->variable()->dimensions().empty()) {
200 dimensions = array->variable()->dimensions();
201 if (dimensions[0].num <= 1 || !dimensions[0].tok) {
202 visitAstNodes(arrayToken,
203 [&](const Token *child) {
204 if (child->originalName() == "->") {
205 mightBeLarger = true;
206 return ChildrenToVisit::none;
207 }
208 return ChildrenToVisit::op1_and_op2;
209 });
210 }
211 } else if (const Token *stringLiteral = array->getValueTokenMinStrSize(settings, &path)) {
212 Dimension dim;
213 dim.tok = nullptr;
214 dim.num = Token::getStrArraySize(stringLiteral);
215 dim.known = array->hasKnownValue();
216 dimensions.emplace_back(dim);
217 } else if (array->valueType() && array->valueType()->pointer >= 1 && (array->valueType()->isIntegral() || array->valueType()->isFloat())) {
218 const ValueFlow::Value *value = getBufferSizeValue(array);
219 if (!value)
220 return false;
221 path = value->path;
222 errorPath = value->errorPath;
223 Dimension dim;
224 dim.known = value->isKnown();
225 dim.tok = nullptr;
226 const auto sizeOf = array->valueType()->pointer > 1 ? ValueType::SizeOf::Pointer : ValueType::SizeOf::Pointee;
227 const size_t typeSize = array->valueType()->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, sizeOf);
228 if (typeSize == 0)
229 return false;
230 dim.num = value->intvalue / typeSize;
231 dimensions.emplace_back(dim);
232 }
233 return !dimensions.empty();
234}
235
236static ValueFlow::Value makeSizeValue(MathLib::bigint size, MathLib::bigint path)
237{

Callers 2

arrayIndexMethod · 0.85
pointerArithmeticMethod · 0.85

Calls 12

visitAstNodesFunction · 0.85
getBufferSizeValueFunction · 0.85
astOperand2Method · 0.80
variableMethod · 0.80
isArrayMethod · 0.80
hasKnownValueMethod · 0.80
isIntegralMethod · 0.80
isKnownMethod · 0.80
getSizeOfMethod · 0.80
emptyMethod · 0.45
isFloatMethod · 0.45

Tested by

no test coverage detected