diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 0de180e9d3f..9b8f4c67595 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -541,6 +541,8 @@ size_t ValueFlow::getSizeOf(const ValueType &vt, const Settings &settings, Accur const size_t charBit = settings.platform.char_bit; size_t n = ValueFlow::getSizeOf(vt2, settings,accuracy, ++maxRecursion); size_t a = getAlignOf(vt2, settings, accuracy); + if (n == 0 || a == 0) + return accuracy == Accuracy::ExactOrZero ? 0 : total; if (bits == 0) { if (currentBitfieldAlloc == 0) { bits = n * charBit; @@ -565,8 +567,6 @@ size_t ValueFlow::getSizeOf(const ValueType &vt, const Settings &settings, Accur currentBitCount += bits; return ret; } - if (n == 0 || a == 0) - return accuracy == Accuracy::ExactOrZero ? 0 : total; n *= dim; size_t padding = (a - (total % a)) % a; if (currentBitCount > 0) { diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 68656d5c356..7a19a92786b 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -176,6 +176,8 @@ class TestValueFlow : public TestFixture { TEST_CASE(performanceIfCount); TEST_CASE(bitfields); + + TEST_CASE(bitfieldsHang); } static bool isNotTokValue(const ValueFlow::Value &val) { @@ -9136,6 +9138,12 @@ class TestValueFlow : public TestFixture { testBitfields("unsigned char a : 16;\n", 2); } + + void bitfieldsHang() { + const char *code = "struct S { unknown_type x : 1; };\n" + "const size_t size = sizeof(S);\n"; + (void)valueOfTok(code, "x"); + } }; REGISTER_TEST(TestValueFlow)