Merge "libutils: check vsnprintf error" into oc-dev am: a03d37d542 am: d8801aaad8 am: d05ecb8d9d am: dc99a094dd am: 64b312bbd8

Original change: https://googleplex-android-review.googlesource.com/c/platform/system/core/+/12254459

Change-Id: I7d40bc3c63aa13c4050662f17b8a0854f8864ce4
This commit is contained in:
Steven Moreland 2020-08-04 23:02:18 +00:00 committed by Automerger Merge Worker
commit 0cb14f1fe7

View file

@ -322,8 +322,14 @@ status_t String8::appendFormatV(const char* fmt, va_list args)
n = vsnprintf(nullptr, 0, fmt, tmp_args);
va_end(tmp_args);
if (n != 0) {
if (n < 0) return UNKNOWN_ERROR;
if (n > 0) {
size_t oldLength = length();
if ((size_t)n > SIZE_MAX - 1 ||
oldLength > SIZE_MAX - (size_t)n - 1) {
return NO_MEMORY;
}
char* buf = lockBuffer(oldLength + n);
if (buf) {
vsnprintf(buf + oldLength, n + 1, fmt, args);