From 5e59652fd387476ac813911145a7f5391e86bcdd Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 3 Jan 2024 11:50:28 +0100 Subject: [PATCH] Add tests for #1644, #3929, #6109 (#5821) --- test/testbufferoverrun.cpp | 13 +++++++++++++ test/testnullpointer.cpp | 9 +++++++++ test/teststring.cpp | 8 ++++++++ 3 files changed, 30 insertions(+) diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 4069a164a..c655c3d8f 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -166,6 +166,7 @@ private: TEST_CASE(array_index_72); // #11784 TEST_CASE(array_index_73); // #11530 TEST_CASE(array_index_74); // #11088 + TEST_CASE(array_index_75); TEST_CASE(array_index_multidim); TEST_CASE(array_index_switch_in_for); TEST_CASE(array_index_for_in_for); // FP: #2634 @@ -1976,6 +1977,18 @@ private: ASSERT_EQUALS("", errout.str()); } + // #1644 + void array_index_75() + { + check("void f() {\n" + " char buf[10];\n" + " int i = 10;\n" + " while (i >= 3)\n" + " buf[i--] = 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5]: (error) Array 'buf[10]' accessed at index 10, which is out of bounds.\n", errout.str()); + } + void array_index_multidim() { check("void f()\n" "{\n" diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index f984b5aac..217e72405 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -2752,6 +2752,15 @@ private: " return 0;\n" "}"); ASSERT_EQUALS("[test.cpp:7]: (error) Null pointer dereference: myNull\n", errout.str()); + + check("struct T { bool g() const; };\n" + "void f(T* p) {\n" + " if (!p)\n" + " return;\n" + " while (p->g())\n" + " p = nullptr;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5]: (warning) Possible null pointer dereference: p\n", errout.str()); } void nullpointer94() // #11040 diff --git a/test/teststring.cpp b/test/teststring.cpp index 8d443e215..b4d8d8156 100644 --- a/test/teststring.cpp +++ b/test/teststring.cpp @@ -798,6 +798,14 @@ private: " ASSERT((void*)(\"def\") == 0);\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("class C {\n" // #6109 + " void check(const char code[], bool validate = true, const char* filename = \"test.cpp\");\n" + " void f() {\n" + " check(\"class A;\", \"test.C\");\n" + " }\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:4]: (warning) Conversion of string literal \"test.C\" to bool always evaluates to true.\n", errout.str()); } void deadStrcmp() {