From 11f1a9d1f5090af4ef33e1a0899da671760d989d Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 2 Jan 2023 17:44:17 +0100 Subject: [PATCH] Fix crash on ternary with omitted operand (#4673) * Fix MSVC compiler warning * Fix crash on incomplete ternary operator * Revert "Fix crash on incomplete ternary operator" This reverts commit 28df0f0ab6ff794e733617447f847a97c1a7a609. * Handle ternary with omitted operand --- lib/valueflow.cpp | 2 ++ test/testcmdlineparser.cpp | 4 ++-- test/testvalueflow.cpp | 6 ++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 60a56fe89..c5097b9b5 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1744,6 +1744,8 @@ static bool isConvertedToIntegral(const Token* tok, const Settings* settings) static bool isSameToken(const Token* tok1, const Token* tok2) { + if (!tok1 || !tok2) + return false; if (tok1->exprId() != 0 && tok1->exprId() == tok2->exprId()) return true; if (tok1->hasKnownIntValue() && tok2->hasKnownIntValue()) diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 9aaa3b090..2dee45329 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -1265,7 +1265,7 @@ private: void valueFlowMaxIterations() { REDIRECT; const char * const argv[] = {"cppcheck", "--valueflow-max-iterations=0"}; - settings.valueFlowMaxIterations = -1; + settings.valueFlowMaxIterations = SIZE_MAX; ASSERT(defParser.parseFromArgs(2, argv)); ASSERT_EQUALS(0, settings.valueFlowMaxIterations); ASSERT_EQUALS("", GET_REDIRECT_OUTPUT); @@ -1274,7 +1274,7 @@ private: void valueFlowMaxIterations2() { REDIRECT; const char * const argv[] = {"cppcheck", "--valueflow-max-iterations=11"}; - settings.valueFlowMaxIterations = -1; + settings.valueFlowMaxIterations = SIZE_MAX; ASSERT(defParser.parseFromArgs(2, argv)); ASSERT_EQUALS(11, settings.valueFlowMaxIterations); ASSERT_EQUALS("", GET_REDIRECT_OUTPUT); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 695f5c8f0..a199aac00 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -6839,6 +6839,12 @@ private: " std::shared_ptr m;\n" "};\n"; valueOfTok(code, "r"); + + code = "void g(int);\n" + "void f(int x, int y) {\n" + " g(x < y ? : 1);\n" + "};\n"; + valueOfTok(code, "?"); } void valueFlowHang() {