Skip to content

Commit 3df629c

Browse files
y-novikovCommit Bot
authored andcommitted
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/+/2453930 Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Yuly Novikov <ynovikov@chromium.org>
1 parent d5fa6ea commit 3df629c

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/common/angleutils.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,26 @@ std::string ArrayIndexString(const std::vector<unsigned int> &indices)
4949

5050
size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char> &outBuffer)
5151
{
52-
// The state of the va_list passed to vsnprintf is undefined after the call, do a copy in case
53-
// we need to grow the buffer.
52+
// The state of the va_list passed to vsnprintf is undefined after the call,
53+
// do a copy since we don't want to interfere with vararg usage inside the caller.
5454
va_list varargCopy;
5555
va_copy(varargCopy, vararg);
5656

5757
// Attempt to just print to the current buffer
5858
int len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, varargCopy);
5959
va_end(varargCopy);
60+
ASSERT(len >= 0);
6061

61-
if (len < 0 || static_cast<size_t>(len) >= outBuffer.size())
62+
if (static_cast<size_t>(len) >= outBuffer.size())
6263
{
63-
// Buffer was not large enough, calculate the required size and resize the buffer
64-
len = vsnprintf(nullptr, 0, fmt, vararg);
64+
// Buffer was not large enough, resize it
6565
outBuffer.resize(len + 1);
6666

6767
// Print again
6868
va_copy(varargCopy, vararg);
6969
len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, varargCopy);
7070
va_end(varargCopy);
71+
ASSERT(len >= 0);
7172
}
72-
ASSERT(len >= 0);
7373
return static_cast<size_t>(len);
7474
}

0 commit comments

Comments
 (0)