Skip to content

Commit d87ee5a

Browse files
committed
Fix overflow-safe reallocation in avifArrayPush
1 parent 81fd8b3 commit d87ee5a

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

src/utils.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <assert.h>
77
#include <math.h>
8+
#include <stdint.h>
89
#include <string.h>
910

1011
float avifRoundf(float v)
@@ -89,6 +90,11 @@ avifBool avifArrayCreate(void * arrayStruct, uint32_t elementSize, uint32_t init
8990
arr->elementSize = elementSize ? elementSize : 1;
9091
arr->count = 0;
9192
arr->capacity = initialCapacity;
93+
if (arr->capacity > SIZE_MAX / arr->elementSize) {
94+
arr->ptr = NULL;
95+
arr->capacity = 0;
96+
return AVIF_FALSE;
97+
}
9298
size_t byteCount = (size_t)arr->elementSize * arr->capacity;
9399
arr->ptr = (uint8_t *)avifAlloc(byteCount);
94100
if (!arr->ptr) {
@@ -111,10 +117,8 @@ void * avifArrayPush(void * arrayStruct)
111117
}
112118

113119
size_t newByteCount = oldByteCount * 2;
114-
115120
uint8_t * newPtr = (uint8_t *)avifAlloc(newByteCount);
116121
if (newPtr == NULL) {
117-
avifFree(oldPtr);
118122
return NULL;
119123
}
120124

0 commit comments

Comments
 (0)