Skip to content

Commit

Permalink
Fix 12031: False positive: uninitialized variable (#5637)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfultz2 committed Dec 10, 2023
1 parent 233e27b commit 243fa66
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 107 deletions.
27 changes: 11 additions & 16 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,31 +107,17 @@ static int getArgumentPos(const Token* ftok, const Token* tokToFind){
return findArgumentPos(startTok, tokToFind);
}

template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
static void astFlattenRecursive(T* tok, std::vector<T*>& result, const char* op, nonneg int depth = 0)
{
++depth;
if (!tok || depth >= 100)
return;
if (tok->str() == op) {
astFlattenRecursive(tok->astOperand1(), result, op, depth);
astFlattenRecursive(tok->astOperand2(), result, op, depth);
} else {
result.push_back(tok);
}
}

std::vector<const Token*> astFlatten(const Token* tok, const char* op)
{
std::vector<const Token*> result;
astFlattenRecursive(tok, result, op);
astFlattenCopy(tok, op, std::back_inserter(result));
return result;
}

std::vector<Token*> astFlatten(Token* tok, const char* op)
{
std::vector<Token*> result;
astFlattenRecursive(tok, result, op);
astFlattenCopy(tok, op, std::back_inserter(result));
return result;
}

Expand Down Expand Up @@ -163,6 +149,15 @@ bool astHasVar(const Token * tok, nonneg int varid)
return astHasVar(tok->astOperand1(), varid) || astHasVar(tok->astOperand2(), varid);
}

bool astHasExpr(const Token* tok, nonneg int exprid)
{
if (!tok)
return false;
if (tok->exprId() == exprid)
return true;
return astHasExpr(tok->astOperand1(), exprid) || astHasExpr(tok->astOperand2(), exprid);
}

static bool astIsCharWithSign(const Token *tok, ValueType::Sign sign)
{
if (!tok)
Expand Down
16 changes: 16 additions & 0 deletions lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,29 @@ const Token* findExpression(const nonneg int exprid,
const std::function<bool(const Token*)>& pred);
const Token* findExpression(const Token* start, const nonneg int exprid);

template<class T, class OuputIterator, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
void astFlattenCopy(T* tok, const char* op, OuputIterator out, nonneg int depth = 100)
{
--depth;
if (!tok || depth < 0)
return;
if (tok->str() == op) {
astFlattenCopy(tok->astOperand1(), op, out, depth);
astFlattenCopy(tok->astOperand2(), op, out, depth);
} else {
*out = tok;
++out;
}
}

std::vector<const Token*> astFlatten(const Token* tok, const char* op);
std::vector<Token*> astFlatten(Token* tok, const char* op);

nonneg int astCount(const Token* tok, const char* op, int depth = 100);

bool astHasToken(const Token* root, const Token * tok);

bool astHasExpr(const Token* tok, nonneg int exprid);
bool astHasVar(const Token * tok, nonneg int varid);

bool astIsPrimitive(const Token* tok);
Expand Down
8 changes: 0 additions & 8 deletions lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@
#include <vector>

namespace {
struct OnExit {
std::function<void()> f;

~OnExit() {
f();
}
};

struct ForwardTraversal {
enum class Progress { Continue, Break, Skip };
enum class Terminate { None, Bail, Inconclusive };
Expand Down
Loading

4 comments on commit 243fa66

@danmar
Copy link
Owner

@danmar danmar commented on 243fa66 Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pfultz2 Thanks! However there are now lots of crashes in daca@home .. at least the crash in strace seems to be caused by this.
http://cppcheck1.osuosl.org:8000/strace

@danmar
Copy link
Owner

@danmar danmar commented on 243fa66 Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a ticket about such a crash also: https://trac.cppcheck.net/ticket/12255

if it's not fixed until tomorrow I will revert this fix.. I would like to release in a few days.

@chrchr-github
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The crash can be prevented by a nullptr check, but I'm not sure that this is the correct fix.

@firewave
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pfultz2 This introduced a performance regression: https://trac.cppcheck.net/ticket/12270

Please sign in to comment.