Fixed #8506 (CPPCheck printing invalid characters in output)

This commit is contained in:
Daniel Marjamäki 2020-09-04 20:43:54 +02:00
parent 1daf1ec108
commit 8027f40418
2 changed files with 20 additions and 1 deletions

View File

@ -1457,7 +1457,23 @@ static std::string stringFromTokenRange(const Token* start, const Token* end)
ret << "unsigned ";
if (tok->isLong() && !tok->isLiteral())
ret << "long ";
if (tok->originalName().empty() || tok->isUnsigned() || tok->isLong()) {
if (tok->tokType() == Token::eString) {
for (unsigned char c: tok->str()) {
if (c == '\n')
ret << "\\n";
else if (c == '\r')
ret << "\\r";
else if (c == '\t')
ret << "\\t";
else if (c >= ' ' && c <= 126)
ret << c;
else {
char str[10];
sprintf(str, "\\x%02x", c);
ret << str;
}
}
} else if (tok->originalName().empty() || tok->isUnsigned() || tok->isLong()) {
ret << tok->str();
} else
ret << tok->originalName();

View File

@ -1116,6 +1116,9 @@ private:
givenACodeSampleToTokenize data5("void f() { return U\"a\"; }");
ASSERT_EQUALS("returnU\"a\"", data5.tokens()->tokAt(5)->expressionString());
givenACodeSampleToTokenize data6("x = \"\\0\\x1\\x2\\x3\\x4\\x5\\x6\\x7\";");
ASSERT_EQUALS("x=\"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\"", data6.tokens()->next()->expressionString());
}
void hasKnownIntValue() {