Fixed #3998 (Tokenizer::simplifyEnum: wrong handling of expressions => crash)

This commit is contained in:
Daniel Marjamäki 2012-07-26 11:12:28 +02:00
parent 87eb2d444c
commit 1a6f7ea9a0
2 changed files with 15 additions and 18 deletions

View File

@ -7048,7 +7048,7 @@ void Tokenizer::simplifyEnum()
// value is previous expression + 1
tok1->insertToken("+");
tok1 = tok1->next();
tok1->insertToken(MathLib::toString<MathLib::bigint>(lastValue));
tok1->insertToken("1");
enumValue = 0;
enumValueStart = valueStart->next();
enumValueEnd = tok1->next();
@ -7072,19 +7072,10 @@ void Tokenizer::simplifyEnum()
enumValueStart = tok1;
enumValueEnd = tok1;
int level = 0;
if (enumValueEnd->str() == "(" ||
enumValueEnd->str() == "[" ||
enumValueEnd->str() == "{")
while (enumValueEnd->next() && (!Token::Match(enumValueEnd->next(), "[},]") || level)) {
if (Token::Match(enumValueEnd, "(|["))
++level;
while (enumValueEnd->next() &&
(!Token::Match(enumValueEnd->next(), "}|,") || level)) {
if (enumValueEnd->next()->str() == "(" ||
enumValueEnd->next()->str() == "[" ||
enumValueEnd->next()->str() == "{")
++level;
else if (enumValueEnd->next()->str() == ")" ||
enumValueEnd->next()->str() == "]" ||
enumValueEnd->next()->str() == "}")
else if (Token::Match(enumValueEnd->next(), "]|)"))
--level;
enumValueEnd = enumValueEnd->next();
@ -7101,11 +7092,11 @@ void Tokenizer::simplifyEnum()
EnumValue ev(enumName, enumValue, enumValueStart, enumValueEnd);
ev.simplify(enumValues);
enumValues[enumName->str()] = ev;
if (ev.start == NULL) {
tok1 = ev.value;
lastEnumValueStart = lastEnumValueEnd = NULL;
lastEnumValueStart = ev.start;
lastEnumValueEnd = ev.end;
if (ev.start == NULL)
lastValue = MathLib::toLongNumber(ev.value->str());
}
tok1 = ev.end ? ev.end : ev.value;
}
}

View File

@ -348,6 +348,7 @@ private:
TEST_CASE(enum29); // ticket #3747 (bitwise or value)
TEST_CASE(enum30); // ticket #3852 (false positive)
TEST_CASE(enum31); // ticket #3934 (calculation in first item)
TEST_CASE(enum32); // ticket #3998 (access violation)
// remove "std::" on some standard functions
TEST_CASE(removestd);
@ -6702,7 +6703,7 @@ private:
"1 + sizeof ( int ) + "
"1 + sizeof ( int ) + 100 + "
"1 + sizeof ( int ) + 100 + 1 + "
"1 + sizeof ( int ) + 100 + 2 + "
"1 + sizeof ( int ) + 100 + 1 + 1 + "
"90 + "
"91 ;";
@ -7033,6 +7034,11 @@ private:
ASSERT_EQUALS("i = 65 ;", checkSimplifyEnum(code));
}
void enum32() { // #3998 - wrong enum simplification => access violation
const char code[] = "enum { x=(32), y=x, z }; { a, z }";
ASSERT_EQUALS("{ a , ( 32 ) + 1 }", checkSimplifyEnum(code));
}
void removestd() {
ASSERT_EQUALS("; strcpy ( a , b ) ;", tok("; std::strcpy(a,b);"));
ASSERT_EQUALS("; strcat ( a , b ) ;", tok("; std::strcat(a,b);"));