From a81427f97f73af5301dafc0de0b560eb626545c0 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Fri, 29 Jan 2021 03:26:57 -0600 Subject: [PATCH] Fix issue 10022: Logical conjunction error between two completely unrelated members (#3094) --- lib/astutils.cpp | 18 ++++++++++++++++-- test/testcondition.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index ecc88d53c..1b17719f9 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1231,13 +1231,27 @@ bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token // TODO: Handle reverse conditions if (Library::isContainerYield(cond1, Library::Container::Yield::EMPTY, "empty") && Library::isContainerYield(cond2->astOperand1(), Library::Container::Yield::SIZE, "size") && - cond1->astOperand1()->astOperand1()->varId() == cond2->astOperand1()->astOperand1()->astOperand1()->varId()) { + isSameExpression(cpp, + true, + cond1->astOperand1()->astOperand1(), + cond2->astOperand1()->astOperand1()->astOperand1(), + library, + pure, + followVar, + errors)) { return !isZeroBoundCond(cond2); } if (Library::isContainerYield(cond2, Library::Container::Yield::EMPTY, "empty") && Library::isContainerYield(cond1->astOperand1(), Library::Container::Yield::SIZE, "size") && - cond2->astOperand1()->astOperand1()->varId() == cond1->astOperand1()->astOperand1()->astOperand1()->varId()) { + isSameExpression(cpp, + true, + cond2->astOperand1()->astOperand1(), + cond1->astOperand1()->astOperand1()->astOperand1(), + library, + pure, + followVar, + errors)) { return !isZeroBoundCond(cond1); } } diff --git a/test/testcondition.cpp b/test/testcondition.cpp index f8ea359d9..e44514716 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -77,6 +77,7 @@ private: TEST_CASE(incorrectLogicOperator12); TEST_CASE(incorrectLogicOperator13); TEST_CASE(incorrectLogicOperator14); + TEST_CASE(incorrectLogicOperator15); TEST_CASE(secondAlwaysTrueFalseWhenFirstTrueError); TEST_CASE(incorrectLogicOp_condSwapping); TEST_CASE(testBug5895); @@ -1564,6 +1565,30 @@ private: ASSERT_EQUALS("", errout.str()); } + void incorrectLogicOperator15() + { + // 10022 + check("struct PipeRoute {\n" + " std::deque points;\n" + " std::deque estimates;\n" + "};\n" + "void CleanPipeRoutes(std::map& pipeRoutes) {\n" + " for (auto it = pipeRoutes.begin(); it != pipeRoutes.end(); ) {\n" + " PipeRoute* curRoute = it->second;\n" + " if (curRoute->points.empty() && curRoute->estimates.size() != 2)\n" + " {\n" + " delete curRoute;\n" + " it = pipeRoutes.erase(it);\n" + " }\n" + " else\n" + " {\n" + " ++it;\n" + " }\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void secondAlwaysTrueFalseWhenFirstTrueError() { check("void f(int x) {\n" " if (x > 5 && x != 1)\n"