From f2238e717b31cf7a2ccec1ec82df0dc0f696ec47 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 7 Mar 2023 12:44:54 +0100 Subject: [PATCH] Fix crash in isTemporary() (#4866) --- lib/astutils.cpp | 2 +- test/testvalueflow.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 87ea72ea1..9dcf6f4b2 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -407,7 +407,7 @@ bool isTemporary(bool cpp, const Token* tok, const Library* library, bool unknow return false; if (Token::simpleMatch(tok, "?")) { const Token* branchTok = tok->astOperand2(); - if (!branchTok->astOperand1()->valueType()) + if (!branchTok->astOperand1() || !branchTok->astOperand1()->valueType()) return false; if (!branchTok->astOperand2()->valueType()) return false; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 2adb4e6ca..e1ff1a95c 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -6870,6 +6870,15 @@ private: " g(x < y ? : 1);\n" "};\n"; valueOfTok(code, "?"); + + code = "struct C {\n" + " explicit C(bool);\n" + " operator bool();\n" + "};\n" + "void f(bool b) {\n" + " const C& c = C(b) ? : C(false);\n" + "};\n"; + valueOfTok(code, "?"); } void valueFlowHang() {