Commit 3df629c5 by Yuly Novikov Committed by Commit Bot

Fix a crash in FormatStringIntoVector

It was wrong to use vararg after vsnprintf() affected it. Luckily, we don't need to call vsnprintf() on vararg, since the previous call of vsnprintf() on varargCopy already gives us the length that we need. Bug: angleproject:5131 Change-Id: Ie9b62e92ef8ab7e06b51e034c99a5fde20c1ceaf Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2453930Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarShahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Yuly Novikov <ynovikov@chromium.org>
parent d5fa6ea9
......@@ -49,26 +49,26 @@ std::string ArrayIndexString(const std::vector<unsigned int> &indices)
size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char> &outBuffer)
{
// The state of the va_list passed to vsnprintf is undefined after the call, do a copy in case
// we need to grow the buffer.
// The state of the va_list passed to vsnprintf is undefined after the call,
// do a copy since we don't want to interfere with vararg usage inside the caller.
va_list varargCopy;
va_copy(varargCopy, vararg);
// Attempt to just print to the current buffer
int len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, varargCopy);
va_end(varargCopy);
ASSERT(len >= 0);
if (len < 0 || static_cast<size_t>(len) >= outBuffer.size())
if (static_cast<size_t>(len) >= outBuffer.size())
{
// Buffer was not large enough, calculate the required size and resize the buffer
len = vsnprintf(nullptr, 0, fmt, vararg);
// Buffer was not large enough, resize it
outBuffer.resize(len + 1);
// Print again
va_copy(varargCopy, vararg);
len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, varargCopy);
va_end(varargCopy);
ASSERT(len >= 0);
}
ASSERT(len >= 0);
return static_cast<size_t>(len);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment