From 785c537ca253894da3bd6fd0155befa032d39b7a Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 7 Dec 2023 12:16:53 +0100 Subject: [PATCH] Add tests for #4527, #10330 (#5734) --- test/testuninitvar.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index ff6490afb85..85d80b991ab 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -6306,6 +6306,34 @@ class TestUninitVar : public TestFixture { " if ((b[0] = g()) == 0) {}\n" "}"); ASSERT_EQUALS("", errout.str()); + + valueFlowUninit("void f(const char *x, char *y);\n" // #4527 + "void g(char* b) {\n" + " char a[1000];\n" + " f(a, b);\n" + " printf(\"%s\", a);\n" + "}"); + ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: a\n", errout.str()); + + valueFlowUninit("void usage(const char *);\n" // #10330 + "int main(int argc, char* argv[]) {\n" + " int user = 0;\n" + " struct passwd* pwd;\n" + " while (1) {\n" + " int c = getc();\n" + " if (c == -1)\n" + " break;\n" + " switch (c) {\n" + " case 'u': user = 123; break;\n" + " }\n" + " }\n" + " if (argc == 1)\n" + " usage(argv[0]);\n" + " if (user)\n" + " pwd = getpwnam(user);\n" + " if (pwd == NULL) {}\n" + "}"); + ASSERT_EQUALS("[test.cpp:15] -> [test.cpp:17]: (warning) Uninitialized variable: pwd\n", errout.str()); } void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value