From 61e1c4183f39ca8d43bb55e4ff79163c8ebab31e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 5 Apr 2010 09:04:30 +0200 Subject: [PATCH] Uninitialized variables: better handling of 'a[b[..]]' when b is not initialized --- lib/checkother.cpp | 4 ++-- test/testother.cpp | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index b155f77cd..9ae03afcb 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1992,9 +1992,9 @@ private: return &tok; } - if (Token::Match(tok.previous(), "[;{}] %var% =")) + if (Token::Match(tok.previous(), "[;{}] %var% =|[")) { - // using same variable rhs? + // check variable usages in rhs/index for (const Token *tok2 = tok.tokAt(2); tok2; tok2 = tok2->next()) { if (Token::Match(tok2, ";|)|=")) diff --git a/test/testother.cpp b/test/testother.cpp index cf1d85f34..b52bd9295 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1603,6 +1603,13 @@ private: // arrays.. void uninitvar_arrays() { + checkUninitVar("int f()\n" + "{\n" + " char a[10];\n" + " a[a[0]] = 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str()); + checkUninitVar("int f()\n" "{\n" " char a[10];\n"