Fixed #716 (segmentation fault: sizeof string)

This commit is contained in:
Daniel Marjamäki 2009-09-22 20:50:00 +02:00
parent 9d65a1ebe8
commit 0e729fedc0
2 changed files with 34 additions and 0 deletions

View File

@ -1291,6 +1291,31 @@ bool Tokenizer::createLinks()
void Tokenizer::simplifySizeof() void Tokenizer::simplifySizeof()
{ {
// Replace 'sizeof(str)'
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (tok->str() != "sizeof")
continue;
if (Token::Match(tok, "sizeof %str%"))
{
tok->deleteThis();
std::ostringstream ostr;
ostr << (strlen(tok->str().c_str()) - 1);
tok->str(ostr.str());
}
if (Token::Match(tok, "sizeof ( %str% )"))
{
tok->deleteThis();
tok->deleteThis();
tok->deleteNext();
std::ostringstream ostr;
ostr << (strlen(tok->str().c_str()) - 1);
tok->str(ostr.str());
}
}
// Fill the map _typeSize.. // Fill the map _typeSize..
_typeSize.clear(); _typeSize.clear();
_typeSize["char"] = sizeof(char); _typeSize["char"] = sizeof(char);

View File

@ -662,6 +662,15 @@ private:
TODO_ASSERT_EQUALS(expected.str(), sizeof_(code)); TODO_ASSERT_EQUALS(expected.str(), sizeof_(code));
} }
// ticket #716 - sizeof string
{
std::ostringstream expected;
expected << "; " << (sizeof "123");
ASSERT_EQUALS(expected.str(), sizeof_("; sizeof \"123\""));
ASSERT_EQUALS(expected.str(), sizeof_("; sizeof(\"123\")"));
}
} }
void casting() void casting()