Skip to content

Commit

Permalink
avoid even more unchecked pointer dereferences / changed some pointer…
Browse files Browse the repository at this point in the history
…s to references (danmar#6296)
  • Loading branch information
firewave authored Apr 15, 2024
1 parent 65f571f commit 087fd79
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 88 deletions.
22 changes: 11 additions & 11 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -823,35 +823,35 @@ void MainWindow::addIncludeDirs(const QStringList &includeDirs, Settings &result
}
}

Library::Error MainWindow::loadLibrary(Library *library, const QString &filename)
Library::Error MainWindow::loadLibrary(Library &library, const QString &filename)
{
Library::Error ret;

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

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

#ifdef FILESDIR
// Try to load the library from FILESDIR/cfg..
const QString filesdir = FILESDIR;
if (!filesdir.isEmpty()) {
ret = library->load(nullptr, (filesdir+"/cfg/"+filename).toLatin1());
ret = library.load(nullptr, (filesdir+"/cfg/"+filename).toLatin1());
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
return ret;
ret = library->load(nullptr, (filesdir+filename).toLatin1());
ret = library.load(nullptr, (filesdir+filename).toLatin1());
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
return ret;
}
Expand All @@ -860,18 +860,18 @@ Library::Error MainWindow::loadLibrary(Library *library, const QString &filename
// Try to load the library from the cfg subfolder..
const QString datadir = getDataDir();
if (!datadir.isEmpty()) {
ret = library->load(nullptr, (datadir+"/"+filename).toLatin1());
ret = library.load(nullptr, (datadir+"/"+filename).toLatin1());
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
return ret;
ret = library->load(nullptr, (datadir+"/cfg/"+filename).toLatin1());
ret = library.load(nullptr, (datadir+"/cfg/"+filename).toLatin1());
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
return ret;
}

return ret;
}

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

const bool std = tryLoadLibrary(&result.library, "std.cfg");
const bool std = tryLoadLibrary(result.library, "std.cfg");
if (!std) {
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"));
return {false, {}};
Expand Down Expand Up @@ -1022,7 +1022,7 @@ QPair<bool,Settings> MainWindow::getCppcheckSettings()
for (const QString& library : libraries) {
result.libraries.emplace_back(library.toStdString());
const QString filename = library + ".cfg";
tryLoadLibrary(&result.library, filename);
tryLoadLibrary(result.library, filename);
}

for (const SuppressionList::Suppression &suppression : mProjectFile->getCheckingSuppressions()) {
Expand Down
4 changes: 2 additions & 2 deletions gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,15 +392,15 @@ private slots:
* @param filename filename (no path)
* @return error code
*/
Library::Error loadLibrary(Library *library, const QString &filename);
Library::Error loadLibrary(Library &library, const QString &filename);

/**
* @brief Tries to load library file, prints message on error
* @param library library to use
* @param filename filename (no path)
* @return True if no error
*/
bool tryLoadLibrary(Library *library, const QString& filename);
bool tryLoadLibrary(Library &library, const QString& filename);

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

Expand Down
33 changes: 15 additions & 18 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,12 @@ Library::Container::Yield astContainerYield(const Token* tok, const Token** ftok
return tok->valueType()->container->getYield(ftok2->str());
}

Library::Container::Yield astFunctionYield(const Token* tok, const Settings* settings, const Token** ftok)
Library::Container::Yield astFunctionYield(const Token* tok, const Settings& settings, const Token** ftok)
{
if (!tok)
return Library::Container::Yield::NO_YIELD;

if (!settings)
return Library::Container::Yield::NO_YIELD;

const auto* function = settings->library.getFunction(tok);
const auto* function = settings.library.getFunction(tok);
if (!function)
return Library::Container::Yield::NO_YIELD;

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

const Token* getParentLifetime(const Token* tok, const Library* library)
const Token* getParentLifetime(const Token* tok, const Library& library)
{
std::vector<const Token*> members = getParentMembers(tok);
if (members.size() < 2)
Expand All @@ -647,7 +644,7 @@ const Token* getParentLifetime(const Token* tok, const Library* library)
return var->isLocal() || var->isArgument();
if (Token::simpleMatch(tok2, "["))
return true;
return isTemporary(tok2, library);
return isTemporary(tok2, &library);
});
if (it == members.rend())
return tok;
Expand Down Expand Up @@ -2127,18 +2124,18 @@ bool isUniqueExpression(const Token* tok)
return isUniqueExpression(tok->astOperand2());
}

static bool isEscaped(const Token* tok, bool functionsScope, const Library* library)
static bool isEscaped(const Token* tok, bool functionsScope, const Library& library)
{
if (library && library->isnoreturn(tok))
if (library.isnoreturn(tok))
return true;
if (functionsScope)
return Token::simpleMatch(tok, "throw");
return Token::Match(tok, "return|throw");
}

static bool isEscapedOrJump(const Token* tok, bool functionsScope, const Library* library)
static bool isEscapedOrJump(const Token* tok, bool functionsScope, const Library& library)
{
if (library && library->isnoreturn(tok))
if (library.isnoreturn(tok))
return true;
if (functionsScope)
return Token::simpleMatch(tok, "throw");
Expand All @@ -2162,7 +2159,7 @@ bool isEscapeFunction(const Token* ftok, const Library* library)
return false;
}

static bool hasNoreturnFunction(const Token* tok, const Library* library, const Token** unknownFunc)
static bool hasNoreturnFunction(const Token* tok, const Library& library, const Token** unknownFunc)
{
if (!tok)
return false;
Expand All @@ -2176,12 +2173,12 @@ static bool hasNoreturnFunction(const Token* tok, const Library* library, const
return true;
if (function->isAttributeNoreturn())
return true;
} else if (library && library->isnoreturn(ftok)) {
} else if (library.isnoreturn(ftok)) {
return true;
} else if (Token::Match(ftok, "exit|abort")) {
return true;
}
if (unknownFunc && !function && library && library->functions.count(library->getFunctionName(ftok)) == 0)
if (unknownFunc && !function && library.functions.count(library.getFunctionName(ftok)) == 0)
*unknownFunc = ftok;
return false;
}
Expand All @@ -2192,7 +2189,7 @@ static bool hasNoreturnFunction(const Token* tok, const Library* library, const
return false;
}

bool isReturnScope(const Token* const endToken, const Library* library, const Token** unknownFunc, bool functionScope)
bool isReturnScope(const Token* const endToken, const Library& library, const Token** unknownFunc, bool functionScope)
{
if (!endToken || endToken->str() != "}")
return false;
Expand Down Expand Up @@ -3005,9 +3002,9 @@ namespace {
};

struct ExpressionChangedSkipDeadCode {
const Library* library;
const Library& library;
const std::function<std::vector<MathLib::bigint>(const Token* tok)>* evaluate;
ExpressionChangedSkipDeadCode(const Library* library,
ExpressionChangedSkipDeadCode(const Library& library,
const std::function<std::vector<MathLib::bigint>(const Token* tok)>& evaluate)
: library(library), evaluate(&evaluate)
{}
Expand Down Expand Up @@ -3036,7 +3033,7 @@ const Token* findExpressionChangedSkipDeadCode(const Token* expr,
int depth)
{
return findExpressionChangedImpl(
expr, start, end, settings, depth, ExpressionChangedSkipDeadCode{&settings->library, evaluate});
expr, start, end, settings, depth, ExpressionChangedSkipDeadCode{settings->library, evaluate});
}

const Token* getArgumentStart(const Token* ftok)
Expand Down
6 changes: 3 additions & 3 deletions lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ bool astIsContainerString(const Token* tok);
Library::Container::Action astContainerAction(const Token* tok, const Token** ftok = nullptr);
Library::Container::Yield astContainerYield(const Token* tok, const Token** ftok = nullptr);

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

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

const Token* getParentLifetime(const Token* tok);
const Token* getParentLifetime(const Token* tok, const Library* library);
const Token* getParentLifetime(const Token* tok, const Library& library);

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

/** Is scope a return scope (scope will unconditionally return) */
CPPCHECKLIB bool isReturnScope(const Token* const endToken,
const Library* library = nullptr,
const Library& library,
const Token** unknownFunc = nullptr,
bool functionScope = false);

Expand Down
2 changes: 1 addition & 1 deletion lib/checkautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
continue;
if (!printInconclusive && val.isInconclusive())
continue;
const Token* parent = getParentLifetime(val.tokvalue, &mSettings->library);
const Token* parent = getParentLifetime(val.tokvalue, mSettings->library);
if (!exprs.insert(parent).second)
continue;
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(parent, escape || isAssignedToNonLocal(tok))) {
Expand Down
4 changes: 2 additions & 2 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3802,10 +3802,10 @@ void CheckOther::checkComparePointers()
continue;
if (var1->isRValueReference() || var2->isRValueReference())
continue;
if (const Token* parent2 = getParentLifetime(v2.tokvalue, &mSettings->library))
if (const Token* parent2 = getParentLifetime(v2.tokvalue, mSettings->library))
if (var1 == parent2->variable())
continue;
if (const Token* parent1 = getParentLifetime(v1.tokvalue, &mSettings->library))
if (const Token* parent1 = getParentLifetime(v1.tokvalue, mSettings->library))
if (var2 == parent1->variable())
continue;
comparePointersError(tok, &v1, &v2);
Expand Down
2 changes: 1 addition & 1 deletion lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ void CheckStl::invalidContainer()
const Scope* s = tok2->scope();
if (!s)
continue;
if (isReturnScope(s->bodyEnd, &mSettings->library))
if (isReturnScope(s->bodyEnd, mSettings->library))
continue;
invalidContainerLoopError(r.ftok, tok, r.errorPath);
bail = true;
Expand Down
10 changes: 5 additions & 5 deletions lib/findtoken.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ template<class T,
class Found,
class Evaluate,
REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
bool findTokensSkipDeadCodeImpl(const Library* library,
bool findTokensSkipDeadCodeImpl(const Library& library,
T* start,
const Token* end,
const Predicate& pred,
Expand Down Expand Up @@ -169,7 +169,7 @@ bool findTokensSkipDeadCodeImpl(const Library* library,
}

template<class T, class Predicate, class Evaluate, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
std::vector<T*> findTokensSkipDeadCode(const Library* library,
std::vector<T*> findTokensSkipDeadCode(const Library& library,
T* start,
const Token* end,
const Predicate& pred,
Expand All @@ -190,13 +190,13 @@ std::vector<T*> findTokensSkipDeadCode(const Library* library,
}

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

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

template<class T, class Predicate, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
T* findTokenSkipDeadCode(const Library* library, T* start, const Token* end, const Predicate& pred)
T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, const Predicate& pred)
{
return findTokenSkipDeadCode(library, start, end, pred, &evaluateKnownValues);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ namespace {
for (const Token* tok=start; tok != end; tok = tok->previous()) {
if (Token::simpleMatch(tok, "}")) {
const Token* ftok = nullptr;
const bool r = isReturnScope(tok, &settings.library, &ftok);
const bool r = isReturnScope(tok, settings.library, &ftok);
if (r)
return true;
}
Expand All @@ -321,7 +321,7 @@ namespace {

bool isEscapeScope(const Token* endBlock, bool& unknown) const {
const Token* ftok = nullptr;
const bool r = isReturnScope(endBlock, &settings.library, &ftok);
const bool r = isReturnScope(endBlock, settings.library, &ftok);
if (!r && ftok)
unknown = true;
return r;
Expand Down
2 changes: 1 addition & 1 deletion lib/fwdanalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const Token *
}
}
tok = bodyStart->link();
if (isReturnScope(tok, &mLibrary))
if (isReturnScope(tok, mLibrary))
return Result(Result::Type::BAILOUT);
if (Token::simpleMatch(tok, "} else {"))
tok = tok->linkAt(2);
Expand Down
Loading

0 comments on commit 087fd79

Please sign in to comment.