From a79dff15ab3c3c56810de4b4c304afc48e727dfe Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 14 Jan 2023 20:14:38 +0100 Subject: [PATCH] Fix #11492 FP uninitvar with try/catch (#4711) --- lib/forwardanalyzer.cpp | 2 +- test/testother.cpp | 13 +++++++++++++ test/testuninitvar.cpp | 12 ++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index 43b311776..be45734a0 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -770,7 +770,7 @@ struct ForwardTraversal { tryTraversal.updateRange(tok->next(), endBlock, depth - 1); bool bail = tryTraversal.actions.isModified(); if (bail) - analyzer->lowerToPossible(); + return Break(); while (Token::simpleMatch(endBlock, "} catch (")) { Token* endCatch = endBlock->linkAt(2); diff --git a/test/testother.cpp b/test/testother.cpp index cc25a16fa..465e6046b 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -265,6 +265,7 @@ private: TEST_CASE(moveCallback); TEST_CASE(moveClassVariable); TEST_CASE(forwardAndUsed); + TEST_CASE(moveAndReference); TEST_CASE(funcArgNamesDifferent); TEST_CASE(funcArgOrderDifferent); @@ -10193,6 +10194,18 @@ private: ASSERT_EQUALS("[test.cpp:4]: (warning) Access of forwarded variable 't'.\n", errout.str()); } + void moveAndReference() { // #9791 + check("void g(std::string&&);\n" + "void h(const std::string&);\n" + "void f() {\n" + " std::string s;\n" + " const std::string& r = s;\n" + " g(std::move(s));\n" + " h(r);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:7]: (warning) Access of moved variable 'r'.\n", errout.str()); + } + void funcArgNamesDifferent() { check("void func1(int a, int b, int c);\n" "void func1(int a, int b, int c) { }\n" diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 50fd36bf7..28d4d16e5 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -5407,6 +5407,18 @@ private: " (void)p[i];\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // #11492 + valueFlowUninit("void f() {\n" + " int i;\n" + " try {\n" + " i = 0;\n" + " }\n" + " catch (...) {\n" + " if (i) {}\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value