From a31c996a700e85aa365ae1ff582c36db436a248f Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 4 May 2023 20:35:56 +0200 Subject: [PATCH] Add tests for #11075, #11382 (#5035) --- test/testnullpointer.cpp | 13 +++++++++++++ test/testuninitvar.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index e013c513c..51d37e6ef 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -141,6 +141,7 @@ private: TEST_CASE(nullpointer98); // #11458 TEST_CASE(nullpointer99); // #10602 TEST_CASE(nullpointer100); // #11636 + TEST_CASE(nullpointer101); // #11382 TEST_CASE(nullpointer_addressOf); // address of TEST_CASE(nullpointerSwitch); // #2626 TEST_CASE(nullpointer_cast); // #4692 @@ -2839,6 +2840,18 @@ private: ASSERT_EQUALS("", errout.str()); } + void nullpointer101() // #11382 + { + check("struct Base { virtual ~Base(); };\n" + "struct Derived : Base {};\n" + "bool is_valid(const Derived&);\n" + "void f(const Base* base) {\n" + " const Derived* derived = dynamic_cast(base);\n" + " if (derived && !is_valid(*derived) || base == nullptr) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void nullpointer_addressOf() { // address of check("void f() {\n" " struct X *x = 0;\n" diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 05d9f0d92..6a9a9d9dd 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -6026,6 +6026,33 @@ private: " g(p);\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + valueFlowUninit("struct T {};\n" // #11075 + "struct S {\n" + " int n;\n" + " struct T t[10];\n" + "};\n" + "void f(struct S* s, char** tokens) {\n" + " struct T t[10];\n" + " int n = 0;\n" + " for (int i = 0; i < s->n; i++)\n" + " if (tokens[i])\n" + " t[n++] = s->t[i];\n" + " for (int i = 0; i < n; i++)\n" + " t[i];\n" + "}\n", "test.c"); + ASSERT_EQUALS("", errout.str()); + + valueFlowUninit("bool g();\n" + "void f() {\n" + " int a[10];\n" + " int idx = 0;\n" + " if (g())\n" + " a[idx++] = 1;\n" + " for (int i = 0; i < idx; i++)\n" + " (void)a[i];\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value