Fix addition/overflow checks.
For unsigned arithmetic, use:
(a + b < a) to detect whether a+b wraps
(a > c / b) to detect whether a*b > c
Bug: 120078455
Test: builds and boots
Change-Id: Ic4f4d44f7c0656caa115f90f4cfa130192914949
(cherry picked from commit da2f2fd0f4
)
This commit is contained in:
parent
876a1960e4
commit
93fe51840e
2 changed files with 10 additions and 3 deletions
|
@ -49,9 +49,10 @@ struct BufferedTextOutput::BufferState : public RefBase
|
|||
}
|
||||
|
||||
status_t append(const char* txt, size_t len) {
|
||||
if (len > SIZE_MAX - bufferPos) return NO_MEMORY; // overflow
|
||||
if ((len+bufferPos) > bufferSize) {
|
||||
if ((len + bufferPos) > SIZE_MAX / 3) return NO_MEMORY; // overflow
|
||||
size_t newSize = ((len+bufferPos)*3)/2;
|
||||
if (newSize < (len+bufferPos)) return NO_MEMORY; // overflow
|
||||
void* b = realloc(buffer, newSize);
|
||||
if (!b) return NO_MEMORY;
|
||||
buffer = (char*)b;
|
||||
|
|
|
@ -520,8 +520,10 @@ status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
|
|||
const sp<ProcessState> proc(ProcessState::self());
|
||||
// grow objects
|
||||
if (mObjectsCapacity < mObjectsSize + numObjects) {
|
||||
if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
|
||||
if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
|
||||
size_t newSize = ((mObjectsSize + numObjects)*3)/2;
|
||||
if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
|
||||
if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
|
||||
binder_size_t *objects =
|
||||
(binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
|
||||
if (objects == (binder_size_t*)nullptr) {
|
||||
|
@ -1399,8 +1401,10 @@ restart_write:
|
|||
if (err != NO_ERROR) return err;
|
||||
}
|
||||
if (!enoughObjects) {
|
||||
if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
|
||||
if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
|
||||
size_t newSize = ((mObjectsSize+2)*3)/2;
|
||||
if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
|
||||
if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
|
||||
binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
|
||||
if (objects == nullptr) return NO_MEMORY;
|
||||
mObjects = objects;
|
||||
|
@ -2685,6 +2689,8 @@ status_t Parcel::growData(size_t len)
|
|||
return BAD_VALUE;
|
||||
}
|
||||
|
||||
if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
|
||||
if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
|
||||
size_t newSize = ((mDataSize+len)*3)/2;
|
||||
return (newSize <= mDataSize)
|
||||
? (status_t) NO_MEMORY
|
||||
|
|
Loading…
Reference in a new issue