Skip to content

Commit 087fd79

Browse files
authored
avoid even more unchecked pointer dereferences / changed some pointers to references (#6296)
1 parent 65f571f commit 087fd79

15 files changed

Lines changed: 83 additions & 88 deletions

gui/mainwindow.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -823,35 +823,35 @@ void MainWindow::addIncludeDirs(const QStringList &includeDirs, Settings &result
823823
}
824824
}
825825

826-
Library::Error MainWindow::loadLibrary(Library *library, const QString &filename)
826+
Library::Error MainWindow::loadLibrary(Library &library, const QString &filename)
827827
{
828828
Library::Error ret;
829829

830830
// Try to load the library from the project folder..
831831
if (mProjectFile) {
832832
QString path = QFileInfo(mProjectFile->getFilename()).canonicalPath();
833-
ret = library->load(nullptr, (path+"/"+filename).toLatin1());
833+
ret = library.load(nullptr, (path+"/"+filename).toLatin1());
834834
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
835835
return ret;
836836
}
837837

838838
// Try to load the library from the application folder..
839839
const QString appPath = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath();
840-
ret = library->load(nullptr, (appPath+"/"+filename).toLatin1());
840+
ret = library.load(nullptr, (appPath+"/"+filename).toLatin1());
841841
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
842842
return ret;
843-
ret = library->load(nullptr, (appPath+"/cfg/"+filename).toLatin1());
843+
ret = library.load(nullptr, (appPath+"/cfg/"+filename).toLatin1());
844844
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
845845
return ret;
846846

847847
#ifdef FILESDIR
848848
// Try to load the library from FILESDIR/cfg..
849849
const QString filesdir = FILESDIR;
850850
if (!filesdir.isEmpty()) {
851-
ret = library->load(nullptr, (filesdir+"/cfg/"+filename).toLatin1());
851+
ret = library.load(nullptr, (filesdir+"/cfg/"+filename).toLatin1());
852852
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
853853
return ret;
854-
ret = library->load(nullptr, (filesdir+filename).toLatin1());
854+
ret = library.load(nullptr, (filesdir+filename).toLatin1());
855855
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
856856
return ret;
857857
}
@@ -860,18 +860,18 @@ Library::Error MainWindow::loadLibrary(Library *library, const QString &filename
860860
// Try to load the library from the cfg subfolder..
861861
const QString datadir = getDataDir();
862862
if (!datadir.isEmpty()) {
863-
ret = library->load(nullptr, (datadir+"/"+filename).toLatin1());
863+
ret = library.load(nullptr, (datadir+"/"+filename).toLatin1());
864864
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
865865
return ret;
866-
ret = library->load(nullptr, (datadir+"/cfg/"+filename).toLatin1());
866+
ret = library.load(nullptr, (datadir+"/cfg/"+filename).toLatin1());
867867
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
868868
return ret;
869869
}
870870

871871
return ret;
872872
}
873873

874-
bool MainWindow::tryLoadLibrary(Library *library, const QString& filename)
874+
bool MainWindow::tryLoadLibrary(Library &library, const QString& filename)
875875
{
876876
const Library::Error error = loadLibrary(library, filename);
877877
if (error.errorcode != Library::ErrorCode::OK) {
@@ -972,7 +972,7 @@ QPair<bool,Settings> MainWindow::getCppcheckSettings()
972972
// default to --check-level=normal for GUI for now
973973
result.setCheckLevel(Settings::CheckLevel::normal);
974974

975-
const bool std = tryLoadLibrary(&result.library, "std.cfg");
975+
const bool std = tryLoadLibrary(result.library, "std.cfg");
976976
if (!std) {
977977
QMessageBox::critical(this, tr("Error"), tr("Failed to load %1. Your Cppcheck installation is broken. You can use --data-dir=<directory> at the command line to specify where this file is located. Please note that --data-dir is supposed to be used by installation scripts and therefore the GUI does not start when it is used, all that happens is that the setting is configured.\n\nAnalysis is aborted.").arg("std.cfg"));
978978
return {false, {}};
@@ -1022,7 +1022,7 @@ QPair<bool,Settings> MainWindow::getCppcheckSettings()
10221022
for (const QString& library : libraries) {
10231023
result.libraries.emplace_back(library.toStdString());
10241024
const QString filename = library + ".cfg";
1025-
tryLoadLibrary(&result.library, filename);
1025+
tryLoadLibrary(result.library, filename);
10261026
}
10271027

10281028
for (const SuppressionList::Suppression &suppression : mProjectFile->getCheckingSuppressions()) {

gui/mainwindow.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,15 +392,15 @@ private slots:
392392
* @param filename filename (no path)
393393
* @return error code
394394
*/
395-
Library::Error loadLibrary(Library *library, const QString &filename);
395+
Library::Error loadLibrary(Library &library, const QString &filename);
396396

397397
/**
398398
* @brief Tries to load library file, prints message on error
399399
* @param library library to use
400400
* @param filename filename (no path)
401401
* @return True if no error
402402
*/
403-
bool tryLoadLibrary(Library *library, const QString& filename);
403+
bool tryLoadLibrary(Library &library, const QString& filename);
404404

405405
QString loadAddon(Settings &settings, const QString &filesDir, const QString &pythonCmd, const QString& addon);
406406

lib/astutils.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,12 @@ Library::Container::Yield astContainerYield(const Token* tok, const Token** ftok
304304
return tok->valueType()->container->getYield(ftok2->str());
305305
}
306306

307-
Library::Container::Yield astFunctionYield(const Token* tok, const Settings* settings, const Token** ftok)
307+
Library::Container::Yield astFunctionYield(const Token* tok, const Settings& settings, const Token** ftok)
308308
{
309309
if (!tok)
310310
return Library::Container::Yield::NO_YIELD;
311311

312-
if (!settings)
313-
return Library::Container::Yield::NO_YIELD;
314-
315-
const auto* function = settings->library.getFunction(tok);
312+
const auto* function = settings.library.getFunction(tok);
316313
if (!function)
317314
return Library::Container::Yield::NO_YIELD;
318315

@@ -635,7 +632,7 @@ static std::vector<const Token*> getParentMembers(const Token* tok)
635632
return result;
636633
}
637634

638-
const Token* getParentLifetime(const Token* tok, const Library* library)
635+
const Token* getParentLifetime(const Token* tok, const Library& library)
639636
{
640637
std::vector<const Token*> members = getParentMembers(tok);
641638
if (members.size() < 2)
@@ -647,7 +644,7 @@ const Token* getParentLifetime(const Token* tok, const Library* library)
647644
return var->isLocal() || var->isArgument();
648645
if (Token::simpleMatch(tok2, "["))
649646
return true;
650-
return isTemporary(tok2, library);
647+
return isTemporary(tok2, &library);
651648
});
652649
if (it == members.rend())
653650
return tok;
@@ -2127,18 +2124,18 @@ bool isUniqueExpression(const Token* tok)
21272124
return isUniqueExpression(tok->astOperand2());
21282125
}
21292126

2130-
static bool isEscaped(const Token* tok, bool functionsScope, const Library* library)
2127+
static bool isEscaped(const Token* tok, bool functionsScope, const Library& library)
21312128
{
2132-
if (library && library->isnoreturn(tok))
2129+
if (library.isnoreturn(tok))
21332130
return true;
21342131
if (functionsScope)
21352132
return Token::simpleMatch(tok, "throw");
21362133
return Token::Match(tok, "return|throw");
21372134
}
21382135

2139-
static bool isEscapedOrJump(const Token* tok, bool functionsScope, const Library* library)
2136+
static bool isEscapedOrJump(const Token* tok, bool functionsScope, const Library& library)
21402137
{
2141-
if (library && library->isnoreturn(tok))
2138+
if (library.isnoreturn(tok))
21422139
return true;
21432140
if (functionsScope)
21442141
return Token::simpleMatch(tok, "throw");
@@ -2162,7 +2159,7 @@ bool isEscapeFunction(const Token* ftok, const Library* library)
21622159
return false;
21632160
}
21642161

2165-
static bool hasNoreturnFunction(const Token* tok, const Library* library, const Token** unknownFunc)
2162+
static bool hasNoreturnFunction(const Token* tok, const Library& library, const Token** unknownFunc)
21662163
{
21672164
if (!tok)
21682165
return false;
@@ -2176,12 +2173,12 @@ static bool hasNoreturnFunction(const Token* tok, const Library* library, const
21762173
return true;
21772174
if (function->isAttributeNoreturn())
21782175
return true;
2179-
} else if (library && library->isnoreturn(ftok)) {
2176+
} else if (library.isnoreturn(ftok)) {
21802177
return true;
21812178
} else if (Token::Match(ftok, "exit|abort")) {
21822179
return true;
21832180
}
2184-
if (unknownFunc && !function && library && library->functions.count(library->getFunctionName(ftok)) == 0)
2181+
if (unknownFunc && !function && library.functions.count(library.getFunctionName(ftok)) == 0)
21852182
*unknownFunc = ftok;
21862183
return false;
21872184
}
@@ -2192,7 +2189,7 @@ static bool hasNoreturnFunction(const Token* tok, const Library* library, const
21922189
return false;
21932190
}
21942191

2195-
bool isReturnScope(const Token* const endToken, const Library* library, const Token** unknownFunc, bool functionScope)
2192+
bool isReturnScope(const Token* const endToken, const Library& library, const Token** unknownFunc, bool functionScope)
21962193
{
21972194
if (!endToken || endToken->str() != "}")
21982195
return false;
@@ -3005,9 +3002,9 @@ namespace {
30053002
};
30063003

30073004
struct ExpressionChangedSkipDeadCode {
3008-
const Library* library;
3005+
const Library& library;
30093006
const std::function<std::vector<MathLib::bigint>(const Token* tok)>* evaluate;
3010-
ExpressionChangedSkipDeadCode(const Library* library,
3007+
ExpressionChangedSkipDeadCode(const Library& library,
30113008
const std::function<std::vector<MathLib::bigint>(const Token* tok)>& evaluate)
30123009
: library(library), evaluate(&evaluate)
30133010
{}
@@ -3036,7 +3033,7 @@ const Token* findExpressionChangedSkipDeadCode(const Token* expr,
30363033
int depth)
30373034
{
30383035
return findExpressionChangedImpl(
3039-
expr, start, end, settings, depth, ExpressionChangedSkipDeadCode{&settings->library, evaluate});
3036+
expr, start, end, settings, depth, ExpressionChangedSkipDeadCode{settings->library, evaluate});
30403037
}
30413038

30423039
const Token* getArgumentStart(const Token* ftok)

lib/astutils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ bool astIsContainerString(const Token* tok);
173173
Library::Container::Action astContainerAction(const Token* tok, const Token** ftok = nullptr);
174174
Library::Container::Yield astContainerYield(const Token* tok, const Token** ftok = nullptr);
175175

176-
Library::Container::Yield astFunctionYield(const Token* tok, const Settings* settings, const Token** ftok = nullptr);
176+
Library::Container::Yield astFunctionYield(const Token* tok, const Settings& settings, const Token** ftok = nullptr);
177177

178178
/** Is given token a range-declaration in a range-based for loop */
179179
bool astIsRangeBasedForDecl(const Token* tok);
@@ -209,7 +209,7 @@ const Token* astParentSkipParens(const Token* tok);
209209
const Token* getParentMember(const Token * tok);
210210

211211
const Token* getParentLifetime(const Token* tok);
212-
const Token* getParentLifetime(const Token* tok, const Library* library);
212+
const Token* getParentLifetime(const Token* tok, const Library& library);
213213

214214
std::vector<ValueType> getParentValueTypes(const Token* tok,
215215
const Settings* settings = nullptr,
@@ -304,7 +304,7 @@ bool isEscapeFunction(const Token* ftok, const Library* library);
304304

305305
/** Is scope a return scope (scope will unconditionally return) */
306306
CPPCHECKLIB bool isReturnScope(const Token* const endToken,
307-
const Library* library = nullptr,
307+
const Library& library,
308308
const Token** unknownFunc = nullptr,
309309
bool functionScope = false);
310310

lib/checkautovariables.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
598598
continue;
599599
if (!printInconclusive && val.isInconclusive())
600600
continue;
601-
const Token* parent = getParentLifetime(val.tokvalue, &mSettings->library);
601+
const Token* parent = getParentLifetime(val.tokvalue, mSettings->library);
602602
if (!exprs.insert(parent).second)
603603
continue;
604604
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(parent, escape || isAssignedToNonLocal(tok))) {

lib/checkother.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3802,10 +3802,10 @@ void CheckOther::checkComparePointers()
38023802
continue;
38033803
if (var1->isRValueReference() || var2->isRValueReference())
38043804
continue;
3805-
if (const Token* parent2 = getParentLifetime(v2.tokvalue, &mSettings->library))
3805+
if (const Token* parent2 = getParentLifetime(v2.tokvalue, mSettings->library))
38063806
if (var1 == parent2->variable())
38073807
continue;
3808-
if (const Token* parent1 = getParentLifetime(v1.tokvalue, &mSettings->library))
3808+
if (const Token* parent1 = getParentLifetime(v1.tokvalue, mSettings->library))
38093809
if (var2 == parent1->variable())
38103810
continue;
38113811
comparePointersError(tok, &v1, &v2);

lib/checkstl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ void CheckStl::invalidContainer()
11211121
const Scope* s = tok2->scope();
11221122
if (!s)
11231123
continue;
1124-
if (isReturnScope(s->bodyEnd, &mSettings->library))
1124+
if (isReturnScope(s->bodyEnd, mSettings->library))
11251125
continue;
11261126
invalidContainerLoopError(r.ftok, tok, r.errorPath);
11271127
bail = true;

lib/findtoken.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ template<class T,
7979
class Found,
8080
class Evaluate,
8181
REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
82-
bool findTokensSkipDeadCodeImpl(const Library* library,
82+
bool findTokensSkipDeadCodeImpl(const Library& library,
8383
T* start,
8484
const Token* end,
8585
const Predicate& pred,
@@ -169,7 +169,7 @@ bool findTokensSkipDeadCodeImpl(const Library* library,
169169
}
170170

171171
template<class T, class Predicate, class Evaluate, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
172-
std::vector<T*> findTokensSkipDeadCode(const Library* library,
172+
std::vector<T*> findTokensSkipDeadCode(const Library& library,
173173
T* start,
174174
const Token* end,
175175
const Predicate& pred,
@@ -190,13 +190,13 @@ std::vector<T*> findTokensSkipDeadCode(const Library* library,
190190
}
191191

192192
template<class T, class Predicate, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
193-
std::vector<T*> findTokensSkipDeadCode(const Library* library, T* start, const Token* end, const Predicate& pred)
193+
std::vector<T*> findTokensSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred)
194194
{
195195
return findTokensSkipDeadCode(library, start, end, pred, &evaluateKnownValues);
196196
}
197197

198198
template<class T, class Predicate, class Evaluate, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
199-
T* findTokenSkipDeadCode(const Library* library, T* start, const Token* end, const Predicate& pred, const Evaluate& evaluate)
199+
T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred, const Evaluate& evaluate)
200200
{
201201
T* result = nullptr;
202202
(void)findTokensSkipDeadCodeImpl(
@@ -213,7 +213,7 @@ T* findTokenSkipDeadCode(const Library* library, T* start, const Token* end, con
213213
}
214214

215215
template<class T, class Predicate, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
216-
T* findTokenSkipDeadCode(const Library* library, T* start, const Token* end, const Predicate& pred)
216+
T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred)
217217
{
218218
return findTokenSkipDeadCode(library, start, end, pred, &evaluateKnownValues);
219219
}

lib/forwardanalyzer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ namespace {
311311
for (const Token* tok=start; tok != end; tok = tok->previous()) {
312312
if (Token::simpleMatch(tok, "}")) {
313313
const Token* ftok = nullptr;
314-
const bool r = isReturnScope(tok, &settings.library, &ftok);
314+
const bool r = isReturnScope(tok, settings.library, &ftok);
315315
if (r)
316316
return true;
317317
}
@@ -321,7 +321,7 @@ namespace {
321321

322322
bool isEscapeScope(const Token* endBlock, bool& unknown) const {
323323
const Token* ftok = nullptr;
324-
const bool r = isReturnScope(endBlock, &settings.library, &ftok);
324+
const bool r = isReturnScope(endBlock, settings.library, &ftok);
325325
if (!r && ftok)
326326
unknown = true;
327327
return r;

lib/fwdanalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const Token *
236236
}
237237
}
238238
tok = bodyStart->link();
239-
if (isReturnScope(tok, &mLibrary))
239+
if (isReturnScope(tok, mLibrary))
240240
return Result(Result::Type::BAILOUT);
241241
if (Token::simpleMatch(tok, "} else {"))
242242
tok = tok->linkAt(2);

0 commit comments

Comments
 (0)