From 8027f404180dee1541dbdd674eaf344aeefeba55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 4 Sep 2020 20:43:54 +0200 Subject: [PATCH] Fixed #8506 (CPPCheck printing invalid characters in output) --- lib/token.cpp | 18 +++++++++++++++++- test/testtoken.cpp | 3 +++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/token.cpp b/lib/token.cpp index 79c43fb12..421257538 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -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(); diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 72161b276..33f4c738f 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -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() {