Skip to content

Commit fbe7b38

Browse files
committed
refs #3452 - asser on unprintable characters in error messages [skip ci]
1 parent f303bf9 commit fbe7b38

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

lib/errorlogger.cpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,45 @@ void ErrorMessage::setmsg(const std::string &msg)
226226
// Even this doesn't cause problems with messages that have multiple
227227
// lines, none of the error messages should end into it.
228228
assert(!endsWith(msg,'\n'));
229-
// bailout on any unprintable characters
230-
assert(std::all_of(msg.cbegin(), msg.cend(), [](char c){
229+
230+
std::string msg2;
231+
232+
const bool allprint = std::all_of(msg.cbegin(), msg.cend(), [](char c){
231233
return std::isprint(c) || c == '\n';
232-
}));
234+
});
235+
if (!allprint) {
236+
// encode any unprintable characters
237+
auto I = msg.begin();
238+
const auto E = msg.end();
239+
while (I != E) {
240+
if (std::isprint(*I) || *I == '\n') {
241+
msg2 += *I;
242+
}
243+
else {
244+
msg2 += '\\';
245+
msg2 += std::to_string(static_cast<int>(*I)); // TODO: pad to three digits
246+
}
247+
++I;
248+
}
249+
}
250+
else {
251+
msg2 = msg;
252+
}
233253

234254
// The summary and verbose message are separated by a newline
235255
// If there is no newline then both the summary and verbose messages
236256
// are the given message
237-
const std::string::size_type pos = msg.find('\n');
257+
const std::string::size_type pos = msg2.find('\n');
238258
const std::string symbolName = mSymbolNames.empty() ? std::string() : mSymbolNames.substr(0, mSymbolNames.find('\n'));
239259
if (pos == std::string::npos) {
240-
mShortMessage = replaceStr(msg, "$symbol", symbolName);
241-
mVerboseMessage = replaceStr(msg, "$symbol", symbolName);
242-
} else if (startsWith(msg,"$symbol:")) {
243-
mSymbolNames += msg.substr(8, pos-7);
244-
setmsg(msg.substr(pos + 1));
260+
mShortMessage = replaceStr(msg2, "$symbol", symbolName);
261+
mVerboseMessage = replaceStr(msg2, "$symbol", symbolName);
262+
} else if (startsWith(msg2,"$symbol:")) {
263+
mSymbolNames += msg2.substr(8, pos-7);
264+
setmsg(msg2.substr(pos + 1));
245265
} else {
246-
mShortMessage = replaceStr(msg.substr(0, pos), "$symbol", symbolName);
247-
mVerboseMessage = replaceStr(msg.substr(pos + 1), "$symbol", symbolName);
266+
mShortMessage = replaceStr(msg2.substr(0, pos), "$symbol", symbolName);
267+
mVerboseMessage = replaceStr(msg2.substr(pos + 1), "$symbol", symbolName);
248268
}
249269
}
250270

0 commit comments

Comments
 (0)