From 2162da1aa75303cd3fc263f32615b4c4cff617d4 Mon Sep 17 00:00:00 2001 From: chrchr Date: Wed, 19 Apr 2023 19:11:09 +0200 Subject: [PATCH 1/3] Add tests for #6925, #11042, #11494 --- test/testbufferoverrun.cpp | 10 ++++++++++ test/testsimplifytypedef.cpp | 9 +++++++-- test/teststl.cpp | 9 +++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 88bde9320bc..15fae01f9cd 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -5250,6 +5250,16 @@ class TestBufferOverrun : public TestFixture { " g(a);\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + ctu("const int a[1] = { 1 };\n" // #11042 + "void g(const int* d) {\n" + " (void)d[2];\n" + "}\n" + "void f() {\n" + " g(a);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:3]: (error) Array index out of bounds; 'd' buffer size is 4 and it is accessed at offset 8.\n", + errout.str()); } void ctu_variable() { diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index ad7164acd28..f0e263b809f 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -3201,8 +3201,8 @@ class TestSimplifyTypedef : public TestFixture { tok(code)); } - void simplifyTypedef140() { // #10798 - { + void simplifyTypedef140() { + { // #10798 const char code[] = "typedef void (*b)();\n" "enum class E { a, b, c };\n"; ASSERT_EQUALS("enum class E { a , b , c } ;", tok(code)); @@ -3212,6 +3212,11 @@ class TestSimplifyTypedef : public TestFixture { "enum class E { A };\n"; ASSERT_EQUALS("enum class E { A } ;", tok(code)); } + { // #11494 + const char code[] = "typedef struct S {} KEY;\n" + "class C { enum E { KEY }; };\n"; + ASSERT_EQUALS("struct S { } ; class C { enum E { KEY } ; } ;", tok(code)); + } } void simplifyTypedef141() { // #10144 diff --git a/test/teststl.cpp b/test/teststl.cpp index 8b88ef26ecc..a62c7af0694 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -4750,6 +4750,15 @@ class TestStl : public TestFixture { " return true;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // #6925 + check("void f(const std::string& s, std::string::iterator i) {\n" + " if (i != s.end() && *(i + 1) == *i) {\n" + " if (i + 1 != s.end()) {}\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition 'i+1!=s.end()' is redundant or there is possible dereference of an invalid iterator: i+1.\n", + errout.str()); } void dereferenceInvalidIterator2() { From 69520fe60b71854b1def7c0df7d2e75bcd5e0c8f Mon Sep 17 00:00:00 2001 From: chrchr Date: Wed, 19 Apr 2023 19:29:40 +0200 Subject: [PATCH 2/3] Format --- test/testsimplifytypedef.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index f0e263b809f..bc4df16a184 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -3201,7 +3201,7 @@ class TestSimplifyTypedef : public TestFixture { tok(code)); } - void simplifyTypedef140() { + void simplifyTypedef140() { { // #10798 const char code[] = "typedef void (*b)();\n" "enum class E { a, b, c };\n"; From 61a8ec7468ecd96b48148049a63db803ce3275de Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 20 Apr 2023 12:24:24 +0200 Subject: [PATCH 3/3] Add tests for # 6561, #6619, #7475, --- test/testuninitvar.cpp | 21 +++++++++++++++++++++ test/testunusedvar.cpp | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index b6e116a2759..d08cd174d53 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -5968,6 +5968,27 @@ class TestUninitVar : public TestFixture { " if (a[0]) {}\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // #6619 + valueFlowUninit("void f() {\n" + " int nok, i;\n" + " for (i = 1; i < 5; i++) {\n" + " if (i == 8)\n" + " nok = 8;\n" + " }\n" + " printf(\"nok = %d\\n\", nok);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:7]: (warning) Uninitialized variable: nok\n", errout.str()); + + // #7475 + valueFlowUninit("struct S {\n" + " int a, b, c;\n" + "} typedef s_t;\n" + "void f() {\n" + " s_t s;\n" + " printf(\"%d\", s.a);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: s.a\n", errout.str()); } void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 4ff19d347a0..1da8e3622da 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -1640,6 +1640,14 @@ class TestUnusedVar : public TestFixture { "\n" "input.skip(sizeof(struct Header));"); ASSERT_EQUALS("", errout.str()); + + checkStructMemberUsage("struct S { int a, b, c; };\n" // #6561 + "int f(FILE * fp) {\n" + " S s;\n" + " ::fread(&s, sizeof(S), 1, fp);\n" + " return s.b;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void structmember16() {