Fix double-use of va_list in AppendFmt

AppendFmt calls vsnprintf twice, first to check the size of the
formatted string, then to write it for real, but it used the same
va_list for both calls, so the second call got an invalid va_list after
the first call had already consumed all its arguments.

This is UB and at least on Linux makes the second call print garbage. I
presume Windows implements va_list differently such that this somehow
worked correctly, because on Linux, all of the dialog items get parsed
into invalid JSON due to this bug, with lines like this (note the
missing second array element and closing bracket):
    "pos" : [ -55947262,
This commit is contained in:
Diomendius
2024-06-07 22:20:34 +12:00
parent 1b18a87495
commit 41b96494c2

View File

@@ -98,6 +98,11 @@ void AppendFmt(std::vector<uint8_t> &array, const char *fmt, ...)
if (resultSize <= 0)
return;
// vsnprintf invalidates the va_list, so we need to
// reinit args so the next call doesn't print garbage.
va_end(args);
va_start(args, fmt);
size_t appendSize = static_cast<size_t>(resultSize);
if (SIZE_MAX == appendSize)