UninitVar: too many warnings (pointer dereference)

This commit is contained in:
Daniel Marjamäki 2021-10-03 18:12:29 +02:00
parent 9f2ddf1623
commit f3d9755e65
5 changed files with 36 additions and 13 deletions

View File

@ -3186,3 +3186,8 @@ bool FwdAnalysis::isEscapedAlias(const Token* expr)
} }
return false; return false;
} }
bool isSizeOfEtc(const Token *tok)
{
return Token::Match(tok, "sizeof|typeof|offsetof|decltype|__typeof__ (");
}

View File

@ -376,4 +376,6 @@ private:
bool mValueFlowKnown; bool mValueFlowKnown;
}; };
bool isSizeOfEtc(const Token *tok);
#endif // astutilsH #endif // astutilsH

View File

@ -50,11 +50,6 @@ namespace {
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool isSizeOfEtc(const Token *tok)
{
return Token::Match(tok, "sizeof|typeof|offsetof|decltype|__typeof__ (");
}
// get ast parent, skip possible address-of and casts // get ast parent, skip possible address-of and casts
static const Token *getAstParentSkipPossibleCastAndAddressOf(const Token *vartok, bool *unknown) static const Token *getAstParentSkipPossibleCastAndAddressOf(const Token *vartok, bool *unknown)
{ {

View File

@ -5,6 +5,7 @@
#include "symboldatabase.h" #include "symboldatabase.h"
#include "token.h" #include "token.h"
#include "valueptr.h" #include "valueptr.h"
#include "checknullpointer.h" // UninitValue => isPointerDeref
#include <algorithm> #include <algorithm>
#include <cstdio> #include <cstdio>
@ -12,8 +13,6 @@
#include <tuple> #include <tuple>
#include <utility> #include <utility>
bool isSizeOfEtc(const Token *tok);
struct OnExit { struct OnExit {
std::function<void()> f; std::function<void()> f;
@ -200,6 +199,11 @@ struct ForwardTraversal {
// uninit value => skip further analysis // uninit value => skip further analysis
auto v = std::find_if(tok->values().begin(), tok->values().end(), std::mem_fn(&ValueFlow::Value::isUninitValue)); auto v = std::find_if(tok->values().begin(), tok->values().end(), std::mem_fn(&ValueFlow::Value::isUninitValue));
if (v != tok->values().end()) { if (v != tok->values().end()) {
if (tok->valueType() && tok->valueType()->pointer > 0) {
bool unknown = false;
if (CheckNullPointer::isPointerDeRef(tok, unknown, settings))
return Break(Analyzer::Terminate::Modified);
}
if (Token::Match(tok->astParent(), "[,(]") && !isSizeOfEtc(tok->astParent()->previous())) if (Token::Match(tok->astParent(), "[,(]") && !isSizeOfEtc(tok->astParent()->previous()))
return Break(Analyzer::Terminate::Modified); return Break(Analyzer::Terminate::Modified);
} }

View File

@ -4661,9 +4661,9 @@ private:
"};\n" "};\n"
"\n" "\n"
"void copy_wcs(wcsstruct *wcsin) {\n" "void copy_wcs(wcsstruct *wcsin) {\n"
" wcsstruct *x;\n" " wcsstruct *x;\n"
" memcpy(wcsin, x, sizeof(wcsstruct));\n" // <- True positive " memcpy(wcsin, x, sizeof(wcsstruct));\n" // <- True positive
" x->wcsprm = NULL;\n" // <- False positive " x->wcsprm = NULL;\n" // <- False positive
"}"; "}";
values = tokenValues(code, "x . wcsprm", ValueFlow::Value::ValueType::UNINIT); values = tokenValues(code, "x . wcsprm", ValueFlow::Value::ValueType::UNINIT);
ASSERT_EQUALS(0, values.size()); ASSERT_EQUALS(0, values.size());
@ -4673,12 +4673,29 @@ private:
"};\n" "};\n"
"\n" "\n"
"void copy_wcs(wcsstruct *wcsin) {\n" "void copy_wcs(wcsstruct *wcsin) {\n"
" wcsstruct *x;\n" " wcsstruct *x;\n"
" sizeof(x);\n" " sizeof(x);\n"
" x->wcsprm = NULL;\n" // <- Warn " x->wcsprm = NULL;\n" // <- Warn
"}"; "}";
values = tokenValues(code, "x . wcsprm", ValueFlow::Value::ValueType::UNINIT); values = tokenValues(code, "x . wcsprm", ValueFlow::Value::ValueType::UNINIT);
ASSERT_EQUALS(1, values.size()); ASSERT_EQUALS(1, values.size());
code = "struct wcsstruct {\n"
" int *wcsprm;\n"
"};\n"
"\n"
"void init_wcs(wcsstruct *x) { if (x->wcsprm != NULL); }\n" // <- False positive
"\n"
"void copy_wcs() {\n"
" wcsstruct *x;\n"
" x->wcsprm = NULL;\n" // <- True positive
" init_wcs(x);\n" // <- False positive
"}";
values = tokenValues(code, "x . wcsprm != NULL", ValueFlow::Value::ValueType::UNINIT);
ASSERT_EQUALS(0, values.size());
values = tokenValues(code, "x )", ValueFlow::Value::ValueType::UNINIT);
ASSERT_EQUALS(0, values.size());
} }
void valueFlowConditionExpressions() { void valueFlowConditionExpressions() {