From e248f7d3e514935ea89c12675efc4377750955ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 3 Jan 2010 18:49:13 +0100 Subject: [PATCH] Fixed #1023 (improve check: Unintialized variable not detected when using +=) --- lib/checkother.cpp | 20 ++++++++++++++++++++ test/testother.cpp | 22 ++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index b673410a1..63ac7ccf3 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1625,6 +1625,26 @@ private: return &tok; } + // += etc + if (Token::Match(tok.previous(), "[;{}]") || Token::Match(tok.tokAt(-2), "[;{}] *")) + { + // goto the equal.. + const Token *eq = tok.next(); + if (eq && eq->str() == "[" && eq->link() && eq->link()->next()) + eq = eq->link()->next(); + + // is it X= + if (Token::Match(eq, "+=|-=|*=|/=|&=|^=") || eq->str() == "|=") + { + if (tok.previous()->str() == "*") + use_pointer(foundError, checks, &tok); + else if (tok.next()->str() == "[") + use_array(foundError, checks, &tok); + else + use(foundError, checks, &tok); + } + } + if (Token::Match(tok.next(), "= malloc|kmalloc") || Token::simpleMatch(tok.next(), "= new char [")) { alloc_pointer(checks, tok.varId()); diff --git a/test/testother.cpp b/test/testother.cpp index 894a41873..8f11a8719 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1184,6 +1184,28 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + // += + checkUninitVar("void f()\n" + "{\n" + " int c;\n" + " c += 2;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str()); + + checkUninitVar("void f()\n" + "{\n" + " char *s = malloc(100);\n" + " *s += 10;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Data is allocated but not initialized: s\n", errout.str()); + + checkUninitVar("void f()\n" + "{\n" + " int a[10];\n" + " a[0] += 10;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str()); + // goto.. checkUninitVar("void foo(int x)\n" "{\n"