From d160d274175f45536528b888b0b3e59a2c9bc082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 23 Aug 2017 22:17:49 +0200 Subject: [PATCH] Fixed #8172 (False positive uninitvar on sizeof *ptr) --- lib/checkuninitvar.cpp | 4 ++++ test/testuninitvar.cpp | 45 ++++++++++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 13e8bd739..ef9cdc699 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1235,6 +1235,10 @@ void CheckUninitVar::valueFlowUninit() if (!scope->isExecutable()) continue; for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { + if (Token::simpleMatch(tok, "sizeof (")) { + tok = tok->linkAt(1); + continue; + } if (!tok->variable() || tok->values().size() != 1U) continue; const ValueFlow::Value &v = tok->values().front(); diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index c06d812c8..89cb85b25 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -91,7 +91,6 @@ private: errout.str(""); // Tokenize.. - settings.experimental = true; settings.debugwarnings = debugwarnings; Tokenizer tokenizer(&settings, this); std::istringstream istr(code); @@ -3823,18 +3822,44 @@ private: check.deadPointer(); } + void valueFlowUninit(const char code[]) { + // Clear the error buffer.. + errout.str(""); + + // Tokenize.. + settings.debugwarnings = false; + settings.experimental = false; + + Tokenizer tokenizer(&settings, this); + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.simplifyTokenList2(); + + // Check for redundant code.. + CheckUninitVar checkuninitvar(&tokenizer, &settings, this); + checkuninitvar.valueFlowUninit(); + } + + void valueFlowUninit() { - checkUninitVar("void f() {\n" - " int x;\n" - " switch (x) {}\n" - "}"); + valueFlowUninit("void f() {\n" + " int x;\n" + " switch (x) {}\n" + "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str()); - checkUninitVar("int f() {\n" - " int x;\n" - " init(x);\n" - " return x;\n" // TODO: inconclusive ? - "}"); + valueFlowUninit("int f() {\n" + " int x;\n" + " init(x);\n" + " return x;\n" // TODO: inconclusive ? + "}"); + ASSERT_EQUALS("", errout.str()); + + valueFlowUninit("void f() {\n" // #8172 + " char **x;\n" + " if (2 < sizeof(*x)) {}\n" + "}"); ASSERT_EQUALS("", errout.str()); }