Skip to content

Commit 3d93fed

Browse files
yoctopucedpgeorge
authored andcommitted
py/objarray: Fix buffer overflow in case of memory allocation failure.
If `array.append()` fails with an exception due to heap exhaustion, the next attempt to grow the buffer will cause a buffer overflow because the free slot count is increased before performing the allocation, and will stay as if the allocation succeeded. Signed-off-by: Yoctopuce <[email protected]>
1 parent 9111fa5 commit 3d93fed

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

py/objarray.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,9 @@ static mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
392392
if (self->free == 0) {
393393
size_t item_sz = mp_binary_get_size('@', self->typecode, NULL);
394394
// TODO: alloc policy
395-
self->free = 8;
396-
self->items = m_renew(byte, self->items, item_sz * self->len, item_sz * (self->len + self->free));
395+
size_t add_cnt = 8;
396+
self->items = m_renew(byte, self->items, item_sz * self->len, item_sz * (self->len + add_cnt));
397+
self->free = add_cnt;
397398
mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz);
398399
}
399400
mp_binary_set_val_array(self->typecode, self->items, self->len, arg);

0 commit comments

Comments
 (0)