Move XML escaping to a separate function

This commit is contained in:
Robbert van der Helm
2021-06-28 12:41:53 +02:00
parent fed7fb84c0
commit 388d9739a9
3 changed files with 40 additions and 27 deletions
+31
View File
@@ -93,6 +93,37 @@ bool pid_running(pid_t pid) {
} }
} }
std::string xml_escape(std::string string) {
// Implementation idea stolen from https://stackoverflow.com/a/5665377
std::string escaped;
escaped.reserve(
static_cast<size_t>(static_cast<double>(string.size()) * 1.1));
for (const char& character : string) {
switch (character) {
case '&':
escaped.append("&amp;");
break;
case '\"':
escaped.append("&quot;");
break;
case '\'':
escaped.append("&apos;");
break;
case '<':
escaped.append("&lt;");
break;
case '>':
escaped.append("&gt;");
break;
default:
escaped.push_back(character);
break;
}
}
return escaped;
}
ScopedFlushToZero::ScopedFlushToZero() noexcept { ScopedFlushToZero::ScopedFlushToZero() noexcept {
old_ftz_mode = _MM_GET_FLUSH_ZERO_MODE(); old_ftz_mode = _MM_GET_FLUSH_ZERO_MODE();
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
+5
View File
@@ -133,6 +133,11 @@ bool is_watchdog_timer_disabled();
*/ */
bool pid_running(pid_t pid); bool pid_running(pid_t pid);
/**
* Escape XML entities within a string. Used inside of desktop notifications.
*/
std::string xml_escape(std::string string);
/** /**
* A RAII wrapper that will temporarily enable the FTZ flag so that denormals * A RAII wrapper that will temporarily enable the FTZ flag so that denormals
* are automatically flushed to zero, returning to whatever the flag was * are automatically flushed to zero, returning to whatever the flag was
+4 -27
View File
@@ -407,37 +407,14 @@ bool send_notification(const std::string& title, const std::string body) {
// I think there's a zero chance that we're going to call this function with // I think there's a zero chance that we're going to call this function with
// anything that even somewhat resembles HTML, but we should still do a // anything that even somewhat resembles HTML, but we should still do a
// basic XML escape anyways. // basic XML escape anyways.
// Implementation idea stolen from https://stackoverflow.com/a/5665377 std::ostringstream formatted_body;
std::string escaped_body{}; formatted_body << xml_escape(body);
escaped_body.reserve(
static_cast<size_t>(static_cast<double>(body.size()) * 1.1));
for (const char& character : body) {
switch (character) {
case '&':
escaped_body.append("&amp;");
break;
case '\"':
escaped_body.append("&quot;");
break;
case '\'':
escaped_body.append("&apos;");
break;
case '<':
escaped_body.append("&lt;");
break;
case '>':
escaped_body.append("&gt;");
break;
default:
escaped_body.push_back(character);
break;
}
}
try { try {
return bp::system(notify_send_path, "--urgency=normal", return bp::system(notify_send_path, "--urgency=normal",
"--expire-time=30000", "--app-name=yabridge", title, "--expire-time=30000", "--app-name=yabridge", title,
escaped_body, bp::posix::use_vfork) == EXIT_SUCCESS; formatted_body.str(),
bp::posix::use_vfork) == EXIT_SUCCESS;
} catch (const boost::process::process_error&) { } catch (const boost::process::process_error&) {
// We will have printed the message to the terminal anyways, so if the // We will have printed the message to the terminal anyways, so if the
// user doesn't have libnotify installed we'll just fail silently // user doesn't have libnotify installed we'll just fail silently