Skip to content

Commit 25247b0

Browse files
committed
Remove redundant post-multiplication overflow check and free the old buffer if
avifAlloc() fails while resizing the array
1 parent 13783aa commit 25247b0

1 file changed

Lines changed: 8 additions & 12 deletions

File tree

src/utils.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,22 @@ void * avifArrayPush(void * arrayStruct)
105105
if (arr->count == arr->capacity) {
106106
uint8_t * oldPtr = arr->ptr;
107107
size_t oldByteCount = (size_t)arr->elementSize * arr->capacity;
108-
108+
109109
// Check for overflow before doubling the allocation size
110110
// If oldByteCount > SIZE_MAX/2, then oldByteCount * 2 would overflow
111111
if (oldByteCount > SIZE_MAX / 2) {
112-
// Cannot safely double the allocation size
113112
return NULL;
114113
}
115-
114+
116115
size_t newByteCount = oldByteCount * 2;
117-
118-
// Additional safety check: verify the multiplication didn't overflow
119-
if (newByteCount < oldByteCount) {
120-
// Overflow occurred despite the check (shouldn't happen, but defense in depth)
121-
return NULL;
122-
}
123-
124-
arr->ptr = (uint8_t *)avifAlloc(newByteCount);
125-
if (arr->ptr == NULL) {
116+
117+
uint8_t * newPtr = (uint8_t *)avifAlloc(newByteCount);
118+
if (newPtr == NULL) {
119+
avifFree(oldPtr);
126120
return NULL;
127121
}
122+
123+
arr->ptr = newPtr;
128124
memset(arr->ptr + oldByteCount, 0, oldByteCount);
129125
memcpy(arr->ptr, oldPtr, oldByteCount);
130126
arr->capacity *= 2;

0 commit comments

Comments
 (0)