From 7b9295ff99f366fec9ba2ea0a12ed17b05b0af7c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 28 Oct 2020 16:04:24 +0100 Subject: [PATCH] lib/yajl/Handle: strip newlines from error messages Closes https://github.com/MusicPlayerDaemon/MPD/issues/981 --- NEWS | 4 ++++ src/lib/yajl/Handle.cxx | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 9654176d5..b8b2a003b 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,10 @@ ver 0.22.2 (not yet released) * database - simple: purge songs and virtual directories for unavailable plugins on update +* input + - qobuz/tidal: fix protocol errors due to newlines in error messages +* playlist + - soundcloud: fix protocol errors due to newlines in error messages * state_file: save on shutdown ver 0.22.1 (2020/10/17) diff --git a/src/lib/yajl/Handle.cxx b/src/lib/yajl/Handle.cxx index fbfc8ac02..6a1741b10 100644 --- a/src/lib/yajl/Handle.cxx +++ b/src/lib/yajl/Handle.cxx @@ -30,6 +30,24 @@ #include "Handle.hxx" #include "util/RuntimeError.hxx" #include "util/ScopeExit.hxx" +#include "util/StringStrip.hxx" + +#include + +/** + * Strip whitespace at the beginning and end and replace newline + * characters which are illegal in the MPD protocol. + */ +static const char * +StripErrorMessage(char *s) noexcept +{ + s = Strip(s); + + while (auto newline = std::strchr(s, '\n')) + *newline = ';'; + + return s; +} namespace Yajl { @@ -41,7 +59,9 @@ Handle::ThrowError() AtScopeExit(this, str) { yajl_free_error(handle, str); }; - throw FormatRuntimeError("Failed to parse JSON: %s", str); + + throw FormatRuntimeError("Failed to parse JSON: %s", + StripErrorMessage((char *)str)); } } // namespace Yajl