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
2 changes: 1 addition & 1 deletion lib/check64bit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static bool is32BitIntegerReturn(const Function* func, const Settings* settings)
if (settings->platform.sizeof_pointer != 8)
return false;
const ValueType* vt = func->arg->valueType();
return vt && vt->pointer == 0 && vt->isIntegral() && vt->typeSize(settings->platform) == 4;
return vt && vt->pointer == 0 && vt->isIntegral() && vt->getSizeOf(*settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer) == 4;
}

void Check64BitPortability::pointerassignment()
Expand Down
13 changes: 7 additions & 6 deletions lib/checkbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static int getMinFormatStringOutputLength(const std::vector<const Token*> &param
std::string digits_string;
bool i_d_x_f_found = false;
int parameterLength = 0;
int inputArgNr = formatStringArgNr;
nonneg int inputArgNr = formatStringArgNr;
for (std::size_t i = 1; i + 1 < formatString.length(); ++i) {
if (formatString[i] == '\\') {
if (i < formatString.length() - 1 && formatString[i + 1] == '0')
Expand Down Expand Up @@ -229,7 +229,8 @@ static bool getDimensionsEtc(const Token * const arrayToken, const Settings &set
Dimension dim;
dim.known = value->isKnown();
dim.tok = nullptr;
const MathLib::bigint typeSize = array->valueType()->typeSize(settings.platform, array->valueType()->pointer > 1);
const auto sizeOf = array->valueType()->pointer > 1 ? ValueType::SizeOf::Pointer : ValueType::SizeOf::Pointee;
const size_t typeSize = array->valueType()->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, sizeOf);
if (typeSize == 0)
return false;
dim.num = value->intvalue / typeSize;
Expand Down Expand Up @@ -585,7 +586,7 @@ ValueFlow::Value CheckBufferOverrun::getBufferSize(const Token *bufTok) const
if (var->isPointerArray())
v.intvalue = dim * mSettings->platform.sizeof_pointer;
else {
const MathLib::bigint typeSize = bufTok->valueType()->typeSize(mSettings->platform);
const size_t typeSize = bufTok->valueType()->getSizeOf(*mSettings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
v.intvalue = dim * typeSize;
}

Expand Down Expand Up @@ -929,7 +930,7 @@ bool CheckBufferOverrun::isCtuUnsafeBufferUsage(const Settings &settings, const
{
if (!offset)
return false;
if (!argtok->valueType() || argtok->valueType()->typeSize(settings.platform) == 0)
if (!argtok->valueType() || argtok->valueType()->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee) == 0)
return false;
const Token *indexTok = nullptr;
if (type == 1 && Token::Match(argtok, "%name% [") && argtok->astParent() == argtok->next() && !Token::simpleMatch(argtok->linkAt(1), "] ["))
Expand All @@ -942,7 +943,7 @@ bool CheckBufferOverrun::isCtuUnsafeBufferUsage(const Settings &settings, const
return false;
if (!indexTok->hasKnownIntValue())
return false;
offset->value = indexTok->getKnownIntValue() * argtok->valueType()->typeSize(settings.platform);
offset->value = indexTok->getKnownIntValue() * argtok->valueType()->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
return true;
}

Expand Down Expand Up @@ -1102,7 +1103,7 @@ void CheckBufferOverrun::objectIndex()
continue;
}
if (obj->valueType() && var->valueType() && (obj->isCast() || (obj->isCpp() && isCPPCast(obj)) || obj->valueType()->pointer)) { // allow cast to a different type
const auto varSize = var->valueType()->typeSize(mSettings->platform);
const auto varSize = var->valueType()->getSizeOf(*mSettings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
if (varSize == 0)
continue;
if (obj->valueType()->type != var->valueType()->type) {
Expand Down
4 changes: 1 addition & 3 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3459,9 +3459,7 @@ void CheckClass::checkReturnByReference()
const bool isView = isContainer && var->valueType()->container->view;
bool warn = isContainer && !isView;
if (!warn && !isView) {
const std::size_t size = ValueFlow::getSizeOf(*var->valueType(),
*mSettings,
ValueFlow::Accuracy::LowerBound);
const std::size_t size = var->valueType()->getSizeOf(*mSettings, ValueType::Accuracy::LowerBound, ValueType::SizeOf::Pointer);
if (size > 2 * mSettings->platform.sizeof_pointer)
warn = true;
}
Expand Down
9 changes: 7 additions & 2 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,12 @@ void CheckLeakAutoVar::leakIfAllocated(const Token *vartok,
}
}

static bool isSafeCast(const ValueType* vt, const Settings& settings)
{
const size_t sizeOf = vt->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
return sizeOf == 0 || sizeOf >= settings.platform.sizeof_pointer;
}

void CheckLeakAutoVar::ret(const Token *tok, VarInfo &varInfo, const bool isEndOfScope)
{
const std::map<int, VarInfo::AllocInfo> &alloctype = varInfo.alloctype;
Expand Down Expand Up @@ -1182,8 +1188,7 @@ void CheckLeakAutoVar::ret(const Token *tok, VarInfo &varInfo, const bool isEndO
while (tok3 && tok3->isCast() &&
(!tok3->valueType() ||
tok3->valueType()->pointer ||
(tok3->valueType()->typeSize(mSettings->platform) == 0) ||
(tok3->valueType()->typeSize(mSettings->platform) >= mSettings->platform.sizeof_pointer)))
isSafeCast(tok3->valueType(), *mSettings)))
tok3 = tok3->astOperand2() ? tok3->astOperand2() : tok3->astOperand1();
if (tok3 && tok3->varId() == varid)
tok2 = tok3->next();
Expand Down
4 changes: 2 additions & 2 deletions lib/checkmemoryleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,8 +1071,8 @@ void CheckMemoryLeakNoVar::checkForUnreleasedInputArgument(const Scope *scope)
const Variable* argvar = tok->function()->getArgumentVar(argnr);
if (!argvar || !argvar->valueType())
continue;
const MathLib::bigint argSize = argvar->valueType()->typeSize(mSettings->platform, /*p*/ true);
if (argSize <= 0 || argSize >= mSettings->platform.sizeof_pointer)
const size_t argSize = argvar->valueType()->getSizeOf(*mSettings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
if (argSize == 0 || argSize >= mSettings->platform.sizeof_pointer)
continue;
}
functionCallLeak(arg, arg->str(), functionName);
Expand Down
20 changes: 10 additions & 10 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ static bool isLargeContainer(const Variable* var, const Settings& settings)
return false;
}
const ValueType vtElem = ValueType::parseDecl(vt->containerTypeToken, settings);
const auto elemSize = std::max<std::size_t>(ValueFlow::getSizeOf(vtElem, settings, ValueFlow::Accuracy::LowerBound), 1);
const auto elemSize = std::max<std::size_t>(vtElem.getSizeOf(settings, ValueType::Accuracy::LowerBound, ValueType::SizeOf::Pointer), 1);
const auto arraySize = var->dimension(0) * elemSize;
return arraySize > maxByValueSize;
}
Expand Down Expand Up @@ -1564,7 +1564,7 @@ void CheckOther::checkPassByReference()
// Ensure that it is a large object.
if (!var->type()->classScope)
inconclusive = true;
else if (!var->valueType() || ValueFlow::getSizeOf(*var->valueType(), *mSettings, ValueFlow::Accuracy::LowerBound) <= 2 * mSettings->platform.sizeof_pointer)
else if (!var->valueType() || var->valueType()->getSizeOf(*mSettings, ValueType::Accuracy::LowerBound, ValueType::SizeOf::Pointer) <= 2 * mSettings->platform.sizeof_pointer)
continue;
}
else
Expand Down Expand Up @@ -3327,7 +3327,8 @@ void CheckOther::checkRedundantCopy()
const Token* varTok = fScope->bodyEnd->tokAt(-2);
if (varTok->variable() && !varTok->variable()->isGlobal() &&
(!varTok->variable()->type() || !varTok->variable()->type()->classScope ||
(varTok->variable()->valueType() && ValueFlow::getSizeOf(*varTok->variable()->valueType(), *mSettings, ValueFlow::Accuracy::LowerBound) > 2 * mSettings->platform.sizeof_pointer)))
(varTok->variable()->valueType() &&
varTok->variable()->valueType()->getSizeOf(*mSettings, ValueType::Accuracy::LowerBound, ValueType::SizeOf::Pointer) > 2 * mSettings->platform.sizeof_pointer)))
redundantCopyError(startTok, startTok->str());
}
}
Expand Down Expand Up @@ -3447,7 +3448,7 @@ void CheckOther::checkIncompleteArrayFill()
if (size == 0 && var->valueType()->pointer)
size = mSettings->platform.sizeof_pointer;
else if (size == 0 && var->valueType())
size = ValueFlow::getSizeOf(*var->valueType(), *mSettings, ValueFlow::Accuracy::LowerBound);
size = var->valueType()->getSizeOf(*mSettings, ValueType::Accuracy::LowerBound, ValueType::SizeOf::Pointer);
const Token* tok3 = tok->next()->astOperand2()->astOperand1()->astOperand1();
if ((size != 1 && size != 100 && size != 0) || var->isPointer()) {
if (printWarning)
Expand Down Expand Up @@ -4430,8 +4431,7 @@ static UnionMember parseUnionMember(const Variable &var,
if (var.isArray()) {
size = var.dimension(0);
} else if (vt != nullptr) {
size = ValueFlow::getSizeOf(*vt, settings,
ValueFlow::Accuracy::ExactOrZero);
size = vt->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
}
return UnionMember(nameToken->str(), size);
}
Expand Down Expand Up @@ -4523,7 +4523,7 @@ static bool getBufAndOffset(const Token *expr, const Token *&buf, MathLib::bigin
bufToken = expr->astOperand1()->astOperand1();
offsetToken = expr->astOperand1()->astOperand2();
if (expr->astOperand1()->valueType())
elementSize = ValueFlow::getSizeOf(*expr->astOperand1()->valueType(), settings, ValueFlow::Accuracy::LowerBound);
elementSize = expr->astOperand1()->valueType()->getSizeOf(settings, ValueType::Accuracy::LowerBound, ValueType::SizeOf::Pointer);
} else if (Token::Match(expr, "+|-") && expr->isBinaryOp()) {
const bool pointer1 = (expr->astOperand1()->valueType() && expr->astOperand1()->valueType()->pointer > 0);
const bool pointer2 = (expr->astOperand2()->valueType() && expr->astOperand2()->valueType()->pointer > 0);
Expand All @@ -4532,13 +4532,13 @@ static bool getBufAndOffset(const Token *expr, const Token *&buf, MathLib::bigin
offsetToken = expr->astOperand2();
auto vt = *expr->astOperand1()->valueType();
--vt.pointer;
elementSize = ValueFlow::getSizeOf(vt, settings, ValueFlow::Accuracy::LowerBound);
elementSize = vt.getSizeOf(settings, ValueType::Accuracy::LowerBound, ValueType::SizeOf::Pointer);
} else if (!pointer1 && pointer2) {
bufToken = expr->astOperand2();
offsetToken = expr->astOperand1();
auto vt = *expr->astOperand2()->valueType();
--vt.pointer;
elementSize = ValueFlow::getSizeOf(vt, settings, ValueFlow::Accuracy::LowerBound);
elementSize = vt.getSizeOf(settings, ValueType::Accuracy::LowerBound, ValueType::SizeOf::Pointer);
} else {
return false;
}
Expand All @@ -4547,7 +4547,7 @@ static bool getBufAndOffset(const Token *expr, const Token *&buf, MathLib::bigin
*offset = 0;
auto vt = *expr->valueType();
--vt.pointer;
elementSize = ValueFlow::getSizeOf(vt, settings, ValueFlow::Accuracy::LowerBound);
elementSize = vt.getSizeOf(settings, ValueType::Accuracy::LowerBound, ValueType::SizeOf::Pointer);
if (elementSize > 0) {
*offset *= elementSize;
if (sizeValue)
Expand Down
8 changes: 2 additions & 6 deletions lib/checktype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,8 @@ static bool checkTypeCombination(ValueType src, ValueType tgt, const Settings& s
src.reference = Reference::None;
tgt.reference = Reference::None;

const std::size_t sizeSrc = ValueFlow::getSizeOf(src,
settings,
ValueFlow::Accuracy::ExactOrZero);
const std::size_t sizeTgt = ValueFlow::getSizeOf(tgt,
settings,
ValueFlow::Accuracy::ExactOrZero);
const std::size_t sizeSrc = src.getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
const std::size_t sizeTgt = tgt.getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
if (!(sizeSrc > 0 && sizeTgt > 0 && sizeSrc < sizeTgt))
return false;

Expand Down
6 changes: 3 additions & 3 deletions lib/clangimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1586,16 +1586,16 @@ static void setValues(const Tokenizer &tokenizer, const SymbolDatabase *symbolDa
return v * dim.num;
});
if (var.valueType())
typeSize += mul * var.valueType()->typeSize(settings.platform, true);
typeSize += mul * var.valueType()->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
}
scope.definedType->sizeOf = typeSize;
}

for (auto *tok = const_cast<Token*>(tokenizer.tokens()); tok; tok = tok->next()) {
if (Token::simpleMatch(tok, "sizeof (")) {
ValueType vt = ValueType::parseDecl(tok->tokAt(2), settings);
const MathLib::bigint sz = vt.typeSize(settings.platform, true);
if (sz <= 0)
const size_t sz = vt.getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
if (sz == 0)
continue;
long long mul = 1;
for (const Token *arrtok = tok->linkAt(1)->previous(); arrtok; arrtok = arrtok->previous()) {
Expand Down
4 changes: 2 additions & 2 deletions lib/ctu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ CTU::FileInfo *CTU::getFileInfo(const Tokenizer &tokenizer)
functionCall.location = FileInfo::Location(tokenizer, tok);
functionCall.callArgNr = argnr + 1;
functionCall.callArgumentExpression = argtok->expressionString();
const auto typeSize = argtok->valueType()->typeSize(tokenizer.getSettings().platform);
const auto typeSize = argtok->valueType()->getSizeOf(tokenizer.getSettings(), ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
functionCall.callArgValue.value = typeSize > 0 ? argtok->variable()->dimension(0) * typeSize : -1;
functionCall.warning = false;
fileInfo->functionCalls.push_back(std::move(functionCall));
Expand All @@ -382,7 +382,7 @@ CTU::FileInfo *CTU::getFileInfo(const Tokenizer &tokenizer)
functionCall.location = FileInfo::Location(tokenizer, tok);
functionCall.callArgNr = argnr + 1;
functionCall.callArgumentExpression = argtok->expressionString();
functionCall.callArgValue.value = argtok->astOperand1()->valueType()->typeSize(tokenizer.getSettings().platform);
functionCall.callArgValue.value = argtok->astOperand1()->valueType()->getSizeOf(tokenizer.getSettings(), ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
functionCall.warning = false;
fileInfo->functionCalls.push_back(std::move(functionCall));
}
Expand Down
Loading
Loading