diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 03a2d93c1..52f529a72 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -235,7 +235,13 @@ std::string ErrorLogger::ErrorMessage::fixInvalidChars(const std::string& raw) } else { std::ostringstream es; // straight cast to (unsigned) doesn't work out. - es << '\\' << std::setbase(8) << std::setw(3) << std::setfill('0') << (unsigned)(unsigned char)*from; + const unsigned uFrom = (unsigned)(unsigned char)*from; +#if 0 + if (uFrom<0x20) + es << "\\XXX"; + else +#endif + es << '\\' << std::setbase(8) << std::setw(3) << std::setfill('0') << uFrom; result += es.str(); } ++from; @@ -270,7 +276,7 @@ std::string ErrorLogger::ErrorMessage::toXML(bool verbose, int version) const printer.OpenElement("error", false); printer.PushAttribute("id", _id.c_str()); printer.PushAttribute("severity", Severity::toString(_severity).c_str()); - printer.PushAttribute("msg", _shortMessage.c_str()); + printer.PushAttribute("msg", fixInvalidChars(_shortMessage).c_str()); printer.PushAttribute("verbose", fixInvalidChars(_verboseMessage).c_str()); if (_cwe) printer.PushAttribute("cwe", _cwe); diff --git a/test/testerrorlogger.cpp b/test/testerrorlogger.cpp index 0782c77f3..570b80f32 100644 --- a/test/testerrorlogger.cpp +++ b/test/testerrorlogger.cpp @@ -220,10 +220,21 @@ private: } void ToXmlV2Encoding() const { - std::list locs; - ErrorMessage msg(locs, Severity::error, "Programming error.\nComparing \"\203\" with \"\003\"", "errorId", false); - const std::string message(" "); - ASSERT_EQUALS(message, msg.toXML(false, 2)); + { + std::list locs; + ErrorMessage msg(locs, Severity::error, "Programming error.\nComparing \"\203\" with \"\003\"", "errorId", false); + const std::string message(" "); + ASSERT_EQUALS(message, msg.toXML(false, 2)); + } + { + const char code1[]="äöü"; + const char code2[]="\x12\x00\x00\x01"; + std::list locs; + ErrorMessage msg1(locs, Severity::error, std::string("Programming error.\nReading \"")+code1+"\"", "errorId", false); + ASSERT_EQUALS(" ", msg1.toXML(false, 2)); + ErrorMessage msg2(locs, Severity::error, std::string("Programming error.\nReading \"")+code2+"\"", "errorId", false); + ASSERT_EQUALS(" ", msg2.toXML(false, 2)); + } } void InconclusiveXml() const {