From 0e729fedc070962d1023d4cd9ad9bdaa8e0eee30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 22 Sep 2009 20:50:00 +0200 Subject: [PATCH] Fixed #716 (segmentation fault: sizeof string) --- src/tokenize.cpp | 25 +++++++++++++++++++++++++ test/testsimplifytokens.cpp | 9 +++++++++ 2 files changed, 34 insertions(+) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 62f7a28ae..c4b1b5b81 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1291,6 +1291,31 @@ bool Tokenizer::createLinks() 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.. _typeSize.clear(); _typeSize["char"] = sizeof(char); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 8fbe0e2e9..f43f08ad8 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -662,6 +662,15 @@ private: 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()