From 811fc0cde01a14bfbe1e5108646ce7b9f59c910d Mon Sep 17 00:00:00 2001 From: chrchr Date: Mon, 24 Apr 2023 15:47:02 +0200 Subject: [PATCH] Fix #1175 uninitialized data: casted to 'int *' and dereferenced --- lib/checkuninitvar.cpp | 2 +- test/testuninitvar.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 622beae8db3..6693bc478ab 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1240,7 +1240,7 @@ const Token* CheckUninitVar::isVariableUsage(bool cpp, const Token *vartok, cons tok = tok->astParent(); } if (Token::simpleMatch(tok->astParent(), "=")) { - if (astIsLhs(tok)) + if (astIsLhs(tok) && (alloc == ARRAY || !derefValue || !derefValue->astOperand1() || !derefValue->astOperand1()->isCast())) return nullptr; if (alloc != NO_ALLOC && astIsRhs(valueExpr)) return nullptr; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index d08cd174d53..5aaf808c76b 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2049,6 +2049,14 @@ class TestUninitVar : public TestFixture { " A* a = new A{};\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // #1175 + checkUninitVar("void f() {\n" + " int* p = new int;\n" + " *((int*)*p) = 42;\n" + " delete p;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p\n", errout.str()); } // class / struct..