File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments