From 75b609eeeccb120819db0c583ecb028c5925fa04 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Thu, 27 Jun 2024 22:09:12 +0200 Subject: [PATCH] Fix #12858 FP knownConditionTrueFalse with switch in try/catch in subfunction --- lib/forwardanalyzer.cpp | 2 +- test/testcondition.cpp | 20 ++++++++++++++++++++ test/testnullpointer.cpp | 3 ++- test/testvalueflow.cpp | 2 +- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index 78c97c869017..3ffa7c28a716 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -811,7 +811,7 @@ namespace { } else if (Token::simpleMatch(tok, "switch (")) { if (updateRecursive(tok->next()->astOperand2()) == Progress::Break) return Break(); - return Break(); + return updateScope(tok->next()->link()->next()->link()); } else if (Token* callTok = callExpr(tok)) { // TODO: Dont traverse tokens a second time if (start != callTok && tok != callTok && updateRecursive(callTok->astOperand1()) == Progress::Break) diff --git a/test/testcondition.cpp b/test/testcondition.cpp index e7709c7fa5e2..5a56ac4f45e2 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4631,6 +4631,26 @@ class TestCondition : public TestFixture { " return;\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("int h();\n" + "bool g() {\n" + " bool b{};\n" + " try {\n" + " int x = h();\n" + " switch (x) {\n" + " default:\n" + " b = true;\n" + " }\n" + " }\n" + " catch (...) {\n" + " b = false;\n" + " }\n" + " return b;\n" + "}\n" + "void f() {\n" + " if (g()) {}\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); } void alwaysTrueSymbolic() diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index a9a2a82043d5..ac54023c9c01 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -3154,7 +3154,8 @@ class TestNullPointer : public TestFixture { " break;\n" " };\n" "}"); - ASSERT_EQUALS("", errout_str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:7]: (warning) Either the condition 'fred==NULL' is redundant or there is possible null pointer dereference: fred.\n", + errout_str()); // #4118 - second if check("void f(char *p) {\n" diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index f417a643b4aa..a779a16f0f16 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -2546,7 +2546,7 @@ class TestValueFlow : public TestFixture { " case A: v = x; break;\n" // <- x is not 34 " }\n" "}"; - ASSERT_EQUALS(false, testValueOfX(code, 8U, 34)); + TODO_ASSERT_EQUALS(false, true, testValueOfX(code, 8U, 34)); // while/for code = "void f() {\n" // #6138