lib/yajl/Handle: strip newlines from error messages

Closes https://github.com/MusicPlayerDaemon/MPD/issues/981
This commit is contained in:
Max Kellermann 2020-10-28 16:04:24 +01:00
parent 5f61d440eb
commit 7b9295ff99
2 changed files with 25 additions and 1 deletions

4
NEWS
View File

@ -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)

View File

@ -30,6 +30,24 @@
#include "Handle.hxx"
#include "util/RuntimeError.hxx"
#include "util/ScopeExit.hxx"
#include "util/StringStrip.hxx"
#include <cstring>
/**
* 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