From 13df5b24134c36d90612382be543672e50f5c0a5 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Tue, 13 Aug 2019 23:32:31 -0500 Subject: [PATCH] Fix FP with negative index and negated condition (#2081) --- lib/valueflow.cpp | 1 + test/teststl.cpp | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 56abc37d4..83c62d247 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3894,6 +3894,7 @@ struct ValueFlowConditionHandler { if (parent && (parent->str() == "!" || Token::simpleMatch(parent, "== false"))) { check_if = !check_if; check_else = !check_else; + std::swap(cond.true_values, cond.false_values); } tok2 = parent; } diff --git a/test/teststl.cpp b/test/teststl.cpp index dd4f8b300..b82e757ae 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -1379,6 +1379,14 @@ private: " v[-11] = 123;\n" "}"); ASSERT_EQUALS("[test.cpp:2]: (error) Array index -11 is out of bounds.\n", errout.str()); + + check("int f(int x, const std::vector& a) {\n" + " if (!(x < 5))\n" + " return a[x - 5];\n" + " else\n" + " return a[4 - x];\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); }