diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 6fd1545e3..2d630183b 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -636,13 +636,15 @@ static Token * valueFlowSetConstantValue(const Token *tok, const Settings *setti setTokenValue(const_cast(tok->next()), value); } else if (tok2->type() && tok2->type()->isEnumType()) { long long size = settings->sizeof_int; - const Token * type = tok2->type()->classScope->enumType; - if (type) { - size = type->str() == "char" ? 1 : - type->str() == "short" ? settings->sizeof_short : - type->str() == "int" ? settings->sizeof_int : - (type->str() == "long" && type->isLong()) ? settings->sizeof_long_long : - type->str() == "long" ? settings->sizeof_long : 0; + if (tok2->type()->classScope) { + const Token * type = tok2->type()->classScope->enumType; + if (type) { + size = type->str() == "char" ? 1 : + type->str() == "short" ? settings->sizeof_short : + type->str() == "int" ? settings->sizeof_int : + (type->str() == "long" && type->isLong()) ? settings->sizeof_long_long : + type->str() == "long" ? settings->sizeof_long : 0; + } } ValueFlow::Value value(size); value.setKnown(); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 08eb19893..8a05587dd 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -77,6 +77,8 @@ private: TEST_CASE(valueFlowFunctionDefaultParameter); TEST_CASE(knownValue); + + TEST_CASE(valueFlowSizeofForwardDeclaredEnum); } bool testValueOfX(const char code[], unsigned int linenr, int value) { @@ -2011,6 +2013,11 @@ private: ASSERT_EQUALS(false, testValueOfX(code, 4U, 7)); ASSERT(value.isKnown()); } + + void valueFlowSizeofForwardDeclaredEnum() { + const char *code = "enum E; sz=sizeof(E);"; + valueOfTok(code, "="); // Don't crash (#7775) + } }; REGISTER_TEST(TestValueFlow)