From 998ffe3c603ad32779f470f09ca317b75f4a06f2 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 2 Feb 2024 11:12:31 +0100 Subject: [PATCH] Another fix for #12393 Stack overflow in accumulateStructMembers() (#5934) --- lib/valueflow.cpp | 4 ++-- test/testvalueflow.cpp | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 6a73ce92e83..f851b2bd794 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1139,7 +1139,7 @@ static size_t bitCeil(size_t x) static size_t getAlignOf(const ValueType& vt, const Settings& settings) { - if (vt.pointer || vt.isPrimitive()) { + if (vt.pointer || vt.reference != Reference::None || vt.isPrimitive()) { auto align = ValueFlow::getSizeOf(vt, settings); return align == 0 ? 0 : bitCeil(align); } @@ -1161,7 +1161,7 @@ static nonneg int getSizeOfType(const Token *typeTok, const Settings &settings) size_t ValueFlow::getSizeOf(const ValueType &vt, const Settings &settings) { - if (vt.pointer) + if (vt.pointer || vt.reference != Reference::None) return settings.platform.sizeof_pointer; if (vt.type == ValueType::Type::BOOL || vt.type == ValueType::Type::CHAR) return 1; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index e160a7fb66a..2c4b5f8eff7 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1448,6 +1448,16 @@ class TestValueFlow : public TestFixture { values = tokenValues(code, "( X )"); ASSERT_EQUALS(1U, values.size()); ASSERT_EQUALS(12, values.back().intvalue); + + code = "struct T;\n" + "struct S { T& r; };\n" + "struct T { S s{ *this }; };\n" + "void f() {\n" + " sizeof(T) == sizeof(void*);\n" + "}\n"; + values = tokenValues(code, "=="); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(1LL, values.back().intvalue); } void valueFlowComma()