diff options
author | Elliott Hughes <enh@google.com> | 2021-06-10 16:42:20 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2021-06-10 16:42:20 -0700 |
commit | 59682761fb4ea24b91292bc53cfe9fbf5e3cfc63 (patch) | |
tree | b0908677474a373189fdca003eb4f9eee1b5cafc /libutils/String8.cpp | |
parent | 8f654d8a99738d096e2a7bf87324a515ec0c33bc (diff) |
Check for overflow in String8::real_append.
Bug: http://b/178822418
Test: new tests
Change-Id: I73631a070ade0689441abe5645ba5a5f64a58675
Diffstat (limited to 'libutils/String8.cpp')
-rw-r--r-- | libutils/String8.cpp | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/libutils/String8.cpp b/libutils/String8.cpp index df160445e..b391b1a18 100644 --- a/libutils/String8.cpp +++ b/libutils/String8.cpp @@ -327,21 +327,23 @@ status_t String8::appendFormatV(const char* fmt, va_list args) return result; } -status_t String8::real_append(const char* other, size_t otherLen) -{ +status_t String8::real_append(const char* other, size_t otherLen) { const size_t myLen = bytes(); - SharedBuffer* buf = SharedBuffer::bufferFromData(mString) - ->editResize(myLen+otherLen+1); - if (buf) { - char* str = (char*)buf->data(); - mString = str; - str += myLen; - memcpy(str, other, otherLen); - str[otherLen] = '\0'; - return OK; + SharedBuffer* buf; + size_t newLen; + if (__builtin_add_overflow(myLen, otherLen, &newLen) || + __builtin_add_overflow(newLen, 1, &newLen) || + (buf = SharedBuffer::bufferFromData(mString)->editResize(newLen)) == nullptr) { + return NO_MEMORY; } - return NO_MEMORY; + + char* str = (char*)buf->data(); + mString = str; + str += myLen; + memcpy(str, other, otherLen); + str[otherLen] = '\0'; + return OK; } char* String8::lockBuffer(size_t size) |