Fixed #7775 (crash at valueFlowSetConstantValue)

This commit is contained in:
Robert Reif 2016-10-26 10:36:02 +02:00 committed by Daniel Marjamäki
parent 08a618c476
commit 4216b26b8c
2 changed files with 16 additions and 7 deletions

View File

@ -636,13 +636,15 @@ static Token * valueFlowSetConstantValue(const Token *tok, const Settings *setti
setTokenValue(const_cast<Token *>(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();

View File

@ -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)