From 7c606c4e3b80ddb0733c5101c6b2dd74ab2211e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 27 Dec 2011 13:16:16 +0100 Subject: [PATCH] Uninitialized variables: Fixed false positives for such code: 'if (cond1) { a=0; } if (cond1) { if (cond2) { use_a; } }' --- lib/checkuninitvar.cpp | 4 ++-- test/testuninitvar.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index fdb8105ab..35d3319f7 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1123,7 +1123,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int // goto the { tok = tok->next()->link()->next(); - bool possibleInitIf(number_of_if > 0); + bool possibleInitIf(number_of_if > 0 || suppressErrors); const bool initif = checkScopeForVariable(tok->next(), varid, ispointer, &possibleInitIf); // goto the } @@ -1143,7 +1143,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int // goto the { tok = tok->tokAt(2); - bool possibleInitElse(number_of_if > 0); + bool possibleInitElse(number_of_if > 0 || suppressErrors); const bool initelse = checkScopeForVariable(tok->next(), varid, ispointer, &possibleInitElse); // goto the } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 2054df935..2b59592fb 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1952,6 +1952,20 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str()); + checkUninitVar2("void f() {\n" + " int a;\n" + " if (x) { a = 0; }\n" + " if (x) { if (y) { a++; } }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + checkUninitVar2("void f() {\n" + " int a;\n" + " if (x) { a = 0; }\n" + " if (x) { if (y) { } else { a++; } }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // asm checkUninitVar2("void f() {\n" " int x;\n"