From da1e2b22be7afb2918994d97c5071e6cdc1382dd Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 13 Apr 2022 12:25:53 +0200 Subject: [PATCH] Add some tests (#4014) * Test for #10454 * Test for #10493 * Test for #10069 * Test for #10228 * Fix test case --- test/testbufferoverrun.cpp | 16 ++++++++++++++++ test/testcondition.cpp | 8 ++++++++ test/testother.cpp | 9 +++++++++ test/teststl.cpp | 15 +++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 5b9528279..613c62d8a 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -3163,6 +3163,22 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + check("void f(int a[10]) {\n" // #10069 + " int i = 0;\n" + " for (i = 0; i < 10; i++)\n" + " a[i] = i * 2;\n" + "}\n" + "void g() {\n" + " int b[5];\n" + " f(b);\n" + " return 0;\n" + "}\n"); + ASSERT_EQUALS("test.cpp:8:warning:Buffer 'b' is too small, the function 'f' expects a bigger buffer in 1st argument\n" + "test.cpp:8:note:Function 'f' is called\n" + "test.cpp:1:note:Declaration of 1st function argument.\n" + "test.cpp:7:note:Passing buffer 'b' to function that is declared here\n" + "test.cpp:8:note:Buffer 'b' is too small, the function 'f' expects a bigger buffer in 1st argument\n", + errout.str()); } void possible_buffer_overrun_1() { // #3035 diff --git a/test/testcondition.cpp b/test/testcondition.cpp index a94a505dc..4f648ed03 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4086,6 +4086,14 @@ private: " return 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Condition '!a==!b' is always false\n", errout.str()); + + // #10454 + check("struct S {\n" + " int f() const { return g() ? 0 : 1; }\n" + " bool g() const { return u == 18446744073709551615ULL; }\n" + " unsigned long long u{};\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); } void alwaysTrueSymbolic() diff --git a/test/testother.cpp b/test/testother.cpp index d154cdd45..93533a7e4 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -7939,6 +7939,15 @@ private: " return y;\n" "}"); ASSERT_EQUALS("", errout.str()); + + // #10228 + check("std::tuple g();\n" + "void h(int);\n" + "void f() {\n" + " auto [a, b] = g();\n" + " auto l = [a = a]() { h(i); };\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void redundantVarAssignment_loop() { diff --git a/test/teststl.cpp b/test/teststl.cpp index 85efdf3f2..206a6dea3 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -3981,6 +3981,21 @@ private: " std::string m;\n" "};\n", /*inconclusive*/ true); ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of c_str(). The value returned by c_str() is invalid after this call.\n", errout.str()); + + check("struct S {\n" // #10493 + " void f(const char** pp);\n" + " std::string s;\n" + "};\n" + "void S::f(const char** pp) {\n" + " try {\n" + " *pp = member.c_str();\n" + " }\n" + " catch (...) {\n" + " s = \"xyz\";\n" + " *pp = member.c_str();\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void uselessCalls() {