Skip to content

Commit 8027f40

Browse files
committed
Fixed #8506 (CPPCheck printing invalid characters in output)
1 parent 1daf1ec commit 8027f40

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/token.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,23 @@ static std::string stringFromTokenRange(const Token* start, const Token* end)
14571457
ret << "unsigned ";
14581458
if (tok->isLong() && !tok->isLiteral())
14591459
ret << "long ";
1460-
if (tok->originalName().empty() || tok->isUnsigned() || tok->isLong()) {
1460+
if (tok->tokType() == Token::eString) {
1461+
for (unsigned char c: tok->str()) {
1462+
if (c == '\n')
1463+
ret << "\\n";
1464+
else if (c == '\r')
1465+
ret << "\\r";
1466+
else if (c == '\t')
1467+
ret << "\\t";
1468+
else if (c >= ' ' && c <= 126)
1469+
ret << c;
1470+
else {
1471+
char str[10];
1472+
sprintf(str, "\\x%02x", c);
1473+
ret << str;
1474+
}
1475+
}
1476+
} else if (tok->originalName().empty() || tok->isUnsigned() || tok->isLong()) {
14611477
ret << tok->str();
14621478
} else
14631479
ret << tok->originalName();

test/testtoken.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,9 @@ class TestToken : public TestFixture {
11161116

11171117
givenACodeSampleToTokenize data5("void f() { return U\"a\"; }");
11181118
ASSERT_EQUALS("returnU\"a\"", data5.tokens()->tokAt(5)->expressionString());
1119+
1120+
givenACodeSampleToTokenize data6("x = \"\\0\\x1\\x2\\x3\\x4\\x5\\x6\\x7\";");
1121+
ASSERT_EQUALS("x=\"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\"", data6.tokens()->next()->expressionString());
11191122
}
11201123

11211124
void hasKnownIntValue() {

0 commit comments

Comments
 (0)