Skip to content

Commit 38eef52

Browse files
Fix #15216 Overlapping warnings uninitvar, uninitStructMember (#9035)
1 parent 604e41a commit 38eef52

4 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎lib/checkuninitvar.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ static std::map<nonneg int, VariableValue> getVariableValues(const Token* tok) {
9898
return ret;
9999
}
100100

101-
bool CheckUninitVarImpl::diag(const Token* tok)
101+
bool CheckUninitVarImpl::diag(const Token* tok, bool insert)
102102
{
103103
if (!tok)
104104
return true;
105105
while (Token::Match(tok->astParent(), "*|&|."))
106106
tok = tok->astParent();
107+
if (!insert)
108+
return mUninitDiags.find(tok) != mUninitDiags.end();
107109
return !mUninitDiags.insert(tok).second;
108110
}
109111

@@ -1620,6 +1622,8 @@ void CheckUninitVarImpl::uninitvarError(const Token* tok, const ValueFlow::Value
16201622

16211623
void CheckUninitVarImpl::uninitStructMemberError(const Token *tok, const std::string &membername)
16221624
{
1625+
if (tok && diag(tok, /*insert*/ false))
1626+
return;
16231627
reportError(tok,
16241628
Severity::error,
16251629
"uninitStructMember",

‎lib/checkuninitvar.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class CPPCHECKLIB CheckUninitVarImpl : public CheckImpl {
9191

9292
enum Alloc : std::uint8_t { NO_ALLOC, NO_CTOR_CALL, CTOR_CALL, ARRAY };
9393

94-
bool diag(const Token* tok);
94+
bool diag(const Token* tok, bool insert = true);
9595
/** Check for uninitialized variables */
9696
void check();
9797
void checkScope(const Scope* scope, const std::set<std::string> &arrayTypeDefs);

‎test/cfg/gtk.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ void g_ascii_formatd_format_test(const gchar *format, int i)
683683
struct {
684684
double x;
685685
} s;
686-
// cppcheck-suppress [uninitvar, uninitStructMember]
686+
// cppcheck-suppress uninitvar
687687
g_ascii_formatd(buf, sizeof(buf), format, s.x);
688688
g_print("%s", buf);
689689
}

‎test/testuninitvar.cpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class TestUninitVar : public TestFixture {
106106
TEST_CASE(isVariableUsageDerefValueflow); // *p
107107

108108
TEST_CASE(uninitvar_memberaccess); // (&(a))->b <=> a.b
109+
TEST_CASE(uninitvar_diag);
109110

110111
// whole program analysis
111112
TEST_CASE(ctuTest);
@@ -132,6 +133,17 @@ class TestUninitVar : public TestFixture {
132133
checkuninitvar.check();
133134
}
134135

136+
#define checkUninitVarAll(...) checkUninitVarAll_(__FILE__, __LINE__, __VA_ARGS__)
137+
template<size_t size>
138+
void checkUninitVarAll_(const char* file, int line, const char (&code)[size]) {
139+
SimpleTokenizer tokenizer(settings, *this, true);
140+
ASSERT_LOC(tokenizer.tokenize(code), file, line);
141+
142+
CheckUninitVarImpl checkuninitvar(&tokenizer, settings, *this);
143+
checkuninitvar.valueFlowUninit();
144+
checkuninitvar.check();
145+
}
146+
135147
// TODO: get rid of this
136148
void checkUninitVar_(const char* file, int line, const std::string& code) {
137149
SimpleTokenizer tokenizer(settings, *this);
@@ -8257,6 +8269,15 @@ class TestUninitVar : public TestFixture {
82578269
ASSERT_EQUALS("", errout_str());
82588270
}
82598271

8272+
void uninitvar_diag() {
8273+
checkUninitVarAll("struct S { int x; };\n" // #15216
8274+
"int f() {\n"
8275+
" S s;\n"
8276+
" return s.x;\n"
8277+
"}\n");
8278+
ASSERT_EQUALS("[test.cpp:4:14]: (error) Uninitialized variable: s.x [uninitvar]\n", errout_str());
8279+
}
8280+
82608281
void ctuTest() {
82618282
ctu("void f(int *p) {\n"
82628283
" a = *p;\n"

0 commit comments

Comments
 (0)