Skip to content

Commit 17c6f40

Browse files
committed
avoid some unchecked pointer dereferences
1 parent 686e28d commit 17c6f40

16 files changed

+175
-174
lines changed

lib/astutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ std::vector<ValueType> getParentValueTypes(const Token* tok, const Settings* set
771771
if (Token::simpleMatch(tok->astParent(), "(") && ftok && !tok->astParent()->isCast() &&
772772
ftok->tokType() != Token::eType)
773773
return {};
774-
if (Token::Match(tok->astParent(), "return|(|{|%assign%") && parent) {
774+
if (parent && Token::Match(tok->astParent(), "return|(|{|%assign%")) {
775775
*parent = tok->astParent();
776776
}
777777
if (tok->astParent()->valueType())

lib/checkclass.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,7 @@ void CheckClass::operatorEqToSelf()
17391739
// find the parameter name
17401740
const Token *rhs = func.argumentList.cbegin()->nameToken();
17411741
const Token* out_ifStatementScopeStart = nullptr;
1742-
if (!hasAssignSelf(&func, rhs, &out_ifStatementScopeStart)) {
1742+
if (!hasAssignSelf(&func, rhs, out_ifStatementScopeStart)) {
17431743
if (hasAllocation(&func, scope))
17441744
operatorEqToSelfError(func.token);
17451745
} else if (out_ifStatementScopeStart != nullptr) {
@@ -1858,7 +1858,7 @@ const Token * CheckClass::getIfStmtBodyStart(const Token *tok, const Token *rhs)
18581858
return nullptr;
18591859
}
18601860

1861-
bool CheckClass::hasAssignSelf(const Function *func, const Token *rhs, const Token **out_ifStatementScopeStart)
1861+
bool CheckClass::hasAssignSelf(const Function *func, const Token *rhs, const Token *&out_ifStatementScopeStart)
18621862
{
18631863
if (!rhs)
18641864
return false;
@@ -1881,7 +1881,7 @@ bool CheckClass::hasAssignSelf(const Function *func, const Token *rhs, const Tok
18811881
if (tok2 && tok2->isUnaryOp("&") && tok2->astOperand1()->str() == rhs->str())
18821882
ret = true;
18831883
if (ret) {
1884-
*out_ifStatementScopeStart = getIfStmtBodyStart(tok2, rhs);
1884+
out_ifStatementScopeStart = getIfStmtBodyStart(tok2, rhs);
18851885
}
18861886
return ret ? ChildrenToVisit::done : ChildrenToVisit::op1_and_op2;
18871887
});
@@ -3399,13 +3399,13 @@ void CheckClass::checkThisUseAfterFree()
33993399

34003400
const Token * freeToken = nullptr;
34013401
std::set<const Function *> callstack;
3402-
checkThisUseAfterFreeRecursive(classScope, &func, &var, std::move(callstack), &freeToken);
3402+
checkThisUseAfterFreeRecursive(classScope, &func, &var, std::move(callstack), freeToken);
34033403
}
34043404
}
34053405
}
34063406
}
34073407

3408-
bool CheckClass::checkThisUseAfterFreeRecursive(const Scope *classScope, const Function *func, const Variable *selfPointer, std::set<const Function *> callstack, const Token **freeToken)
3408+
bool CheckClass::checkThisUseAfterFreeRecursive(const Scope *classScope, const Function *func, const Variable *selfPointer, std::set<const Function *> callstack, const Token *&freeToken)
34093409
{
34103410
if (!func || !func->functionScope)
34113411
return false;
@@ -3418,23 +3418,23 @@ bool CheckClass::checkThisUseAfterFreeRecursive(const Scope *classScope, const F
34183418
const Token * const bodyStart = func->functionScope->bodyStart;
34193419
const Token * const bodyEnd = func->functionScope->bodyEnd;
34203420
for (const Token *tok = bodyStart; tok != bodyEnd; tok = tok->next()) {
3421-
const bool isDestroyed = *freeToken != nullptr && !func->isStatic();
3421+
const bool isDestroyed = freeToken != nullptr && !func->isStatic();
34223422
if (Token::Match(tok, "delete %var% ;") && selfPointer == tok->next()->variable()) {
3423-
*freeToken = tok;
3423+
freeToken = tok;
34243424
tok = tok->tokAt(2);
34253425
} else if (Token::Match(tok, "%var% . reset ( )") && selfPointer == tok->variable())
3426-
*freeToken = tok;
3426+
freeToken = tok;
34273427
else if (Token::Match(tok->previous(), "!!. %name% (") && tok->function() && tok->function()->nestedIn == classScope) {
34283428
if (isDestroyed) {
3429-
thisUseAfterFree(selfPointer->nameToken(), *freeToken, tok);
3429+
thisUseAfterFree(selfPointer->nameToken(), freeToken, tok);
34303430
return true;
34313431
}
34323432
if (checkThisUseAfterFreeRecursive(classScope, tok->function(), selfPointer, callstack, freeToken))
34333433
return true;
34343434
} else if (isDestroyed && Token::Match(tok->previous(), "!!. %name%") && tok->variable() && tok->variable()->scope() == classScope && !tok->variable()->isStatic() && !tok->variable()->isArgument()) {
3435-
thisUseAfterFree(selfPointer->nameToken(), *freeToken, tok);
3435+
thisUseAfterFree(selfPointer->nameToken(), freeToken, tok);
34363436
return true;
3437-
} else if (*freeToken && Token::Match(tok, "return|throw")) {
3437+
} else if (freeToken && Token::Match(tok, "return|throw")) {
34383438
// TODO
34393439
return tok->str() == "throw";
34403440
} else if (tok->str() == "{" && tok->scope()->type == Scope::ScopeType::eLambda) {

lib/checkclass.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ class CPPCHECKLIB CheckClass : public Check {
298298
bool hasAllocation(const Function *func, const Scope* scope) const;
299299
bool hasAllocation(const Function *func, const Scope* scope, const Token *start, const Token *end) const;
300300
bool hasAllocationInIfScope(const Function *func, const Scope* scope, const Token *ifStatementScopeStart) const;
301-
static bool hasAssignSelf(const Function *func, const Token *rhs, const Token **out_ifStatementScopeStart);
301+
static bool hasAssignSelf(const Function *func, const Token *rhs, const Token *&out_ifStatementScopeStart);
302302
enum class Bool { TRUE, FALSE, BAILOUT };
303303
static Bool isInverted(const Token *tok, const Token *rhs);
304304
static const Token * getIfStmtBodyStart(const Token *tok, const Token *rhs);
@@ -411,7 +411,7 @@ class CPPCHECKLIB CheckClass : public Check {
411411
/**
412412
* @brief Helper for checkThisUseAfterFree
413413
*/
414-
bool checkThisUseAfterFreeRecursive(const Scope *classScope, const Function *func, const Variable *selfPointer, std::set<const Function *> callstack, const Token **freeToken);
414+
bool checkThisUseAfterFreeRecursive(const Scope *classScope, const Function *func, const Variable *selfPointer, std::set<const Function *> callstack, const Token *&freeToken);
415415
};
416416
/// @}
417417
//---------------------------------------------------------------------------

lib/checkcondition.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,8 +1573,8 @@ void CheckCondition::alwaysTrueFalse()
15731573
{
15741574
const ValueFlow::Value *zeroValue = nullptr;
15751575
const Token *nonZeroExpr = nullptr;
1576-
if (CheckOther::comparisonNonZeroExpressionLessThanZero(tok, &zeroValue, &nonZeroExpr, /*suppress*/ true) ||
1577-
CheckOther::testIfNonZeroExpressionIsPositive(tok, &zeroValue, &nonZeroExpr))
1576+
if (CheckOther::comparisonNonZeroExpressionLessThanZero(tok, zeroValue, nonZeroExpr, /*suppress*/ true) ||
1577+
CheckOther::testIfNonZeroExpressionIsPositive(tok, zeroValue, nonZeroExpr))
15781578
continue;
15791579
}
15801580

lib/checkio.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -487,16 +487,16 @@ void CheckIO::invalidScanfError(const Token *tok)
487487
//---------------------------------------------------------------------------
488488

489489
static bool findFormat(nonneg int arg, const Token *firstArg,
490-
const Token **formatStringTok, const Token **formatArgTok)
490+
const Token *&formatStringTok, const Token *&formatArgTok)
491491
{
492492
const Token* argTok = firstArg;
493493

494494
for (int i = 0; i < arg && argTok; ++i)
495495
argTok = argTok->nextArgument();
496496

497497
if (Token::Match(argTok, "%str% [,)]")) {
498-
*formatArgTok = argTok->nextArgument();
499-
*formatStringTok = argTok;
498+
formatArgTok = argTok->nextArgument();
499+
formatStringTok = argTok;
500500
return true;
501501
}
502502
if (Token::Match(argTok, "%var% [,)]") &&
@@ -506,13 +506,13 @@ static bool findFormat(nonneg int arg, const Token *firstArg,
506506
(argTok->variable()->dimensions().size() == 1 &&
507507
argTok->variable()->dimensionKnown(0) &&
508508
argTok->variable()->dimension(0) != 0))) {
509-
*formatArgTok = argTok->nextArgument();
509+
formatArgTok = argTok->nextArgument();
510510
if (!argTok->values().empty()) {
511511
const std::list<ValueFlow::Value>::const_iterator value = std::find_if(
512512
argTok->values().cbegin(), argTok->values().cend(), std::mem_fn(&ValueFlow::Value::isTokValue));
513513
if (value != argTok->values().cend() && value->isTokValue() && value->tokvalue &&
514514
value->tokvalue->tokType() == Token::eString) {
515-
*formatStringTok = value->tokvalue;
515+
formatStringTok = value->tokvalue;
516516
}
517517
}
518518
return true;
@@ -552,37 +552,37 @@ void CheckIO::checkWrongPrintfScanfArguments()
552552

553553
if (formatStringArgNo >= 0) {
554554
// formatstring found in library. Find format string and first argument belonging to format string.
555-
if (!findFormat(formatStringArgNo, tok->tokAt(2), &formatStringTok, &argListTok))
555+
if (!findFormat(formatStringArgNo, tok->tokAt(2), formatStringTok, argListTok))
556556
continue;
557557
} else if (Token::simpleMatch(tok, "swprintf (")) {
558558
if (Token::Match(tok->tokAt(2)->nextArgument(), "%str%")) {
559559
// Find third parameter and format string
560-
if (!findFormat(1, tok->tokAt(2), &formatStringTok, &argListTok))
560+
if (!findFormat(1, tok->tokAt(2), formatStringTok, argListTok))
561561
continue;
562562
} else {
563563
// Find fourth parameter and format string
564-
if (!findFormat(2, tok->tokAt(2), &formatStringTok, &argListTok))
564+
if (!findFormat(2, tok->tokAt(2), formatStringTok, argListTok))
565565
continue;
566566
}
567567
} else if (isWindows && Token::Match(tok, "sprintf_s|swprintf_s (")) {
568568
// template <size_t size> int sprintf_s(char (&buffer)[size], const char *format, ...);
569-
if (findFormat(1, tok->tokAt(2), &formatStringTok, &argListTok)) {
569+
if (findFormat(1, tok->tokAt(2), formatStringTok, argListTok)) {
570570
if (!formatStringTok)
571571
continue;
572572
}
573573
// int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
574-
else if (findFormat(2, tok->tokAt(2), &formatStringTok, &argListTok)) {
574+
else if (findFormat(2, tok->tokAt(2), formatStringTok, argListTok)) {
575575
if (!formatStringTok)
576576
continue;
577577
}
578578
} else if (isWindows && Token::Match(tok, "_snprintf_s|_snwprintf_s (")) {
579579
// template <size_t size> int _snprintf_s(char (&buffer)[size], size_t count, const char *format, ...);
580-
if (findFormat(2, tok->tokAt(2), &formatStringTok, &argListTok)) {
580+
if (findFormat(2, tok->tokAt(2), formatStringTok, argListTok)) {
581581
if (!formatStringTok)
582582
continue;
583583
}
584584
// int _snprintf_s(char *buffer, size_t sizeOfBuffer, size_t count, const char *format, ...);
585-
else if (findFormat(3, tok->tokAt(2), &formatStringTok, &argListTok)) {
585+
else if (findFormat(3, tok->tokAt(2), formatStringTok, argListTok)) {
586586
if (!formatStringTok)
587587
continue;
588588
}

lib/checkother.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,13 +2714,13 @@ void CheckOther::checkSignOfUnsignedVariable()
27142714
for (const Token *tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
27152715
const ValueFlow::Value *zeroValue = nullptr;
27162716
const Token *nonZeroExpr = nullptr;
2717-
if (comparisonNonZeroExpressionLessThanZero(tok, &zeroValue, &nonZeroExpr)) {
2717+
if (comparisonNonZeroExpressionLessThanZero(tok, zeroValue, nonZeroExpr)) {
27182718
const ValueType* vt = nonZeroExpr->valueType();
27192719
if (vt->pointer)
27202720
pointerLessThanZeroError(tok, zeroValue);
27212721
else
27222722
unsignedLessThanZeroError(tok, zeroValue, nonZeroExpr->expressionString());
2723-
} else if (testIfNonZeroExpressionIsPositive(tok, &zeroValue, &nonZeroExpr)) {
2723+
} else if (testIfNonZeroExpressionIsPositive(tok, zeroValue, nonZeroExpr)) {
27242724
const ValueType* vt = nonZeroExpr->valueType();
27252725
if (vt->pointer)
27262726
pointerPositiveError(tok, zeroValue);
@@ -2731,7 +2731,7 @@ void CheckOther::checkSignOfUnsignedVariable()
27312731
}
27322732
}
27332733

2734-
bool CheckOther::comparisonNonZeroExpressionLessThanZero(const Token *tok, const ValueFlow::Value **zeroValue, const Token **nonZeroExpr, bool suppress)
2734+
bool CheckOther::comparisonNonZeroExpressionLessThanZero(const Token *tok, const ValueFlow::Value *&zeroValue, const Token *&nonZeroExpr, bool suppress)
27352735
{
27362736
if (!tok->isComparisonOp() || !tok->astOperand1() || !tok->astOperand2())
27372737
return false;
@@ -2740,24 +2740,24 @@ bool CheckOther::comparisonNonZeroExpressionLessThanZero(const Token *tok, const
27402740
const ValueFlow::Value *v2 = tok->astOperand2()->getValue(0);
27412741

27422742
if (Token::Match(tok, "<|<=") && v2 && v2->isKnown()) {
2743-
*zeroValue = v2;
2744-
*nonZeroExpr = tok->astOperand1();
2743+
zeroValue = v2;
2744+
nonZeroExpr = tok->astOperand1();
27452745
} else if (Token::Match(tok, ">|>=") && v1 && v1->isKnown()) {
2746-
*zeroValue = v1;
2747-
*nonZeroExpr = tok->astOperand2();
2746+
zeroValue = v1;
2747+
nonZeroExpr = tok->astOperand2();
27482748
} else {
27492749
return false;
27502750
}
27512751

2752-
if (const Variable* var = (*nonZeroExpr)->variable())
2752+
if (const Variable* var = nonZeroExpr->variable())
27532753
if (var->typeStartToken()->isTemplateArg())
27542754
return suppress;
27552755

2756-
const ValueType* vt = (*nonZeroExpr)->valueType();
2756+
const ValueType* vt = nonZeroExpr->valueType();
27572757
return vt && (vt->pointer || vt->sign == ValueType::UNSIGNED);
27582758
}
27592759

2760-
bool CheckOther::testIfNonZeroExpressionIsPositive(const Token *tok, const ValueFlow::Value **zeroValue, const Token **nonZeroExpr)
2760+
bool CheckOther::testIfNonZeroExpressionIsPositive(const Token *tok, const ValueFlow::Value *&zeroValue, const Token *&nonZeroExpr)
27612761
{
27622762
if (!tok->isComparisonOp() || !tok->astOperand1() || !tok->astOperand2())
27632763
return false;
@@ -2766,16 +2766,16 @@ bool CheckOther::testIfNonZeroExpressionIsPositive(const Token *tok, const Value
27662766
const ValueFlow::Value *v2 = tok->astOperand2()->getValue(0);
27672767

27682768
if (Token::simpleMatch(tok, ">=") && v2 && v2->isKnown()) {
2769-
*zeroValue = v2;
2770-
*nonZeroExpr = tok->astOperand1();
2769+
zeroValue = v2;
2770+
nonZeroExpr = tok->astOperand1();
27712771
} else if (Token::simpleMatch(tok, "<=") && v1 && v1->isKnown()) {
2772-
*zeroValue = v1;
2773-
*nonZeroExpr = tok->astOperand2();
2772+
zeroValue = v1;
2773+
nonZeroExpr = tok->astOperand2();
27742774
} else {
27752775
return false;
27762776
}
27772777

2778-
const ValueType* vt = (*nonZeroExpr)->valueType();
2778+
const ValueType* vt = nonZeroExpr->valueType();
27792779
return vt && (vt->pointer || vt->sign == ValueType::UNSIGNED);
27802780
}
27812781

@@ -3864,7 +3864,7 @@ void CheckOther::checkModuloOfOneError(const Token *tok)
38643864
//-----------------------------------------------------------------------------
38653865
// Overlapping write (undefined behavior)
38663866
//-----------------------------------------------------------------------------
3867-
static bool getBufAndOffset(const Token *expr, const Token **buf, MathLib::bigint *offset)
3867+
static bool getBufAndOffset(const Token *expr, const Token *&buf, MathLib::bigint *offset)
38683868
{
38693869
if (!expr)
38703870
return false;
@@ -3885,7 +3885,7 @@ static bool getBufAndOffset(const Token *expr, const Token **buf, MathLib::bigin
38853885
return false;
38863886
}
38873887
} else if (expr->valueType() && expr->valueType()->pointer > 0) {
3888-
*buf = expr;
3888+
buf = expr;
38893889
*offset = 0;
38903890
return true;
38913891
} else {
@@ -3895,7 +3895,7 @@ static bool getBufAndOffset(const Token *expr, const Token **buf, MathLib::bigin
38953895
return false;
38963896
if (!offsetToken->hasKnownIntValue())
38973897
return false;
3898-
*buf = bufToken;
3898+
buf = bufToken;
38993899
*offset = offsetToken->getKnownIntValue();
39003900
return true;
39013901
}
@@ -3974,9 +3974,9 @@ void CheckOther::checkOverlappingWrite()
39743974
const MathLib::bigint sizeValue = args[nonOverlappingData->sizeArg-1]->getKnownIntValue();
39753975
const Token *buf1, *buf2;
39763976
MathLib::bigint offset1, offset2;
3977-
if (!getBufAndOffset(ptr1, &buf1, &offset1))
3977+
if (!getBufAndOffset(ptr1, buf1, &offset1))
39783978
continue;
3979-
if (!getBufAndOffset(ptr2, &buf2, &offset2))
3979+
if (!getBufAndOffset(ptr2, buf2, &offset2))
39803980
continue;
39813981

39823982
if (offset1 < offset2 && offset1 + sizeValue <= offset2)

lib/checkother.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ class CPPCHECKLIB CheckOther : public Check {
5656
CheckOther() : Check(myName()) {}
5757

5858
/** Is expression a comparison that checks if a nonzero (unsigned/pointer) expression is less than zero? */
59-
static bool comparisonNonZeroExpressionLessThanZero(const Token *tok, const ValueFlow::Value **zeroValue, const Token **nonZeroExpr, bool suppress = false);
59+
static bool comparisonNonZeroExpressionLessThanZero(const Token *tok, const ValueFlow::Value *&zeroValue, const Token *&nonZeroExpr, bool suppress = false);
6060

6161
/** Is expression a comparison that checks if a nonzero (unsigned/pointer) expression is positive? */
62-
static bool testIfNonZeroExpressionIsPositive(const Token *tok, const ValueFlow::Value **zeroValue, const Token **nonZeroExpr);
62+
static bool testIfNonZeroExpressionIsPositive(const Token *tok, const ValueFlow::Value *&zeroValue, const Token *&nonZeroExpr);
6363

6464
private:
6565
/** @brief This constructor is used when running checks. */

lib/ctu.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ std::list<CTU::FileInfo::UnsafeUsage> CTU::loadUnsafeUsageListFromXml(const tiny
276276
return ret;
277277
}
278278

279-
static int isCallFunction(const Scope *scope, int argnr, const Token **tok)
279+
static int isCallFunction(const Scope *scope, int argnr, const Token *&tok)
280280
{
281281
const Variable * const argvar = scope->function->getArgumentVar(argnr);
282282
if (!argvar->isPointer())
@@ -299,7 +299,7 @@ static int isCallFunction(const Scope *scope, int argnr, const Token **tok)
299299
break;
300300
if (!prev->astOperand1() || !prev->astOperand1()->function())
301301
break;
302-
*tok = prev->previous();
302+
tok = prev->previous();
303303
return argnr2;
304304
}
305305
return -1;
@@ -424,7 +424,7 @@ CTU::FileInfo *CTU::getFileInfo(const Tokenizer *tokenizer)
424424
// Nested function calls
425425
for (int argnr = 0; argnr < scopeFunction->argCount(); ++argnr) {
426426
const Token *tok;
427-
const int argnr2 = isCallFunction(&scope, argnr, &tok);
427+
const int argnr2 = isCallFunction(&scope, argnr, tok);
428428
if (argnr2 > 0) {
429429
FileInfo::NestedCall nestedCall(tokenizer, scopeFunction, tok);
430430
nestedCall.myArgNr = argnr + 1;

lib/programmemory.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ void ProgramMemory::setIntValue(const Token* expr, MathLib::bigint value, bool i
9797
setValue(expr, v);
9898
}
9999

100-
bool ProgramMemory::getTokValue(nonneg int exprid, const Token** result) const
100+
bool ProgramMemory::getTokValue(nonneg int exprid, const Token*& result) const
101101
{
102102
const ValueFlow::Value* value = getValue(exprid);
103103
if (value && value->isTokValue()) {
104-
*result = value->tokvalue;
104+
result = value->tokvalue;
105105
return true;
106106
}
107107
return false;
@@ -1469,7 +1469,7 @@ namespace {
14691469
return lhs;
14701470
} else if (expr->str() == "[" && expr->astOperand1() && expr->astOperand2()) {
14711471
const Token* tokvalue = nullptr;
1472-
if (!pm->getTokValue(expr->astOperand1()->exprId(), &tokvalue)) {
1472+
if (!pm->getTokValue(expr->astOperand1()->exprId(), tokvalue)) {
14731473
auto tokvalue_it = std::find_if(expr->astOperand1()->values().cbegin(),
14741474
expr->astOperand1()->values().cend(),
14751475
std::mem_fn(&ValueFlow::Value::isTokValue));

0 commit comments

Comments
 (0)