From 44404b20aad2f6aa5ddb49a1c8015fc7b23a2c2a Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Wed, 29 Oct 2025 19:58:45 +0100 Subject: [PATCH 1/2] Fix #14231 Crash in checkConstVariable() --- lib/checkother.cpp | 2 +- test/testother.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 04365271c6e..7542e7d7b3a 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1723,7 +1723,7 @@ void CheckOther::checkConstVariable() if (retTok->varId() == var->declarationId()) return true; while (retTok && retTok->isCast()) - retTok = retTok->astOperand2(); + retTok = retTok->astOperand2() ? retTok->astOperand2() : retTok->astOperand1(); while (Token::simpleMatch(retTok, ".")) retTok = retTok->astOperand2(); if (Token::simpleMatch(retTok, "&")) diff --git a/test/testother.cpp b/test/testother.cpp index 28c812325d7..2da1a8318b1 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3967,6 +3967,13 @@ class TestOther : public TestFixture { check("size_t* f(std::array& a) { return reinterpret_cast(a.data()); }\n"); // #14074 ASSERT_EQUALS("", errout_str()); + + check("struct S { int i; };\n" // #14231 + "void* f(S& s, int& v) {\n" + " v = s.i;\n" + " return (void*)&s;\n" + "}\n"); // don't crash + ASSERT_EQUALS("", errout_str()); } void constParameterCallback() { From 43286138cf923f82cda0e5b1559c4d5accb14349 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Wed, 29 Oct 2025 20:00:48 +0100 Subject: [PATCH 2/2] Format --- test/testother.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/testother.cpp b/test/testother.cpp index 2da1a8318b1..d1fc966d6ff 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3969,10 +3969,10 @@ class TestOther : public TestFixture { ASSERT_EQUALS("", errout_str()); check("struct S { int i; };\n" // #14231 - "void* f(S& s, int& v) {\n" - " v = s.i;\n" - " return (void*)&s;\n" - "}\n"); // don't crash + "void* f(S& s, int& v) {\n" + " v = s.i;\n" + " return (void*)&s;\n" + "}\n"); // don't crash ASSERT_EQUALS("", errout_str()); }