From 03559cec5b10545a1923677b60faa925966c0b6d Mon Sep 17 00:00:00 2001 From: Julio Merino Date: Wed, 13 Feb 2013 23:00:55 -0500 Subject: [PATCH] Do not write unprintable characters in XML reports Convert some unprintable characters to a *plain-text* representation in XML reports. This is a little hack but does the trick for now. The NetBSD test suite has started spitting out a character with value 27, and that confuses xsltproc when processing an XML-based report created by atf-report. Even converting the character to  doesn't seem to work. --- NEWS | 5 +++++ atf-report/atf-report.cpp | 22 +++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 9f793373..197f40a4 100644 --- a/NEWS +++ b/NEWS @@ -30,6 +30,11 @@ STILL UNDER DEVELOPMENT; NOT RELEASED YET. executing a test program. This should help crossbuild systems by providing a mechanism to pre-specify what the results should be. +* PR bin/45690: Make atf-report convert any non-printable characters to + a plain-text representation (matching their corresponding hexadecimal + entities) in XML output files. This is to prevent the output of test + cases from breaking xsltproc later. + Changes in version 0.16 *********************** diff --git a/atf-report/atf-report.cpp b/atf-report/atf-report.cpp index bcc70408..359b58b2 100644 --- a/atf-report/atf-report.cpp +++ b/atf-report/atf-report.cpp @@ -31,6 +31,7 @@ extern "C" { #include } +#include #include #include #include @@ -394,17 +395,24 @@ class xml_writer : public writer { std::string elemval(const std::string& str) { - std::string ostr; + std::ostringstream buf; for (std::string::const_iterator iter = str.begin(); iter != str.end(); iter++) { - switch (*iter) { - case '&': ostr += "&"; break; - case '<': ostr += "<"; break; - case '>': ostr += ">"; break; - default: ostr += *iter; + const int character = static_cast< unsigned char >(*iter); + if (character == '&') { + buf << "&"; + } else if (character == '<') { + buf << "<"; + } else if (character == '>') { + buf << ">"; + } else if (std::isalnum(character) || std::ispunct(character) || + std::isspace(character)) { + buf << static_cast< char >(character); + } else { + buf << "&#" << character << ";"; } } - return ostr; + return buf.str(); } void