@@ -409,12 +409,12 @@ void *gc_alloc(size_t n_bytes, bool has_finaliser) {
409409 void * ret_ptr = (void * )(MP_STATE_MEM (gc_pool_start ) + start_block * BYTES_PER_BLOCK );
410410 DEBUG_printf ("gc_alloc(%p)\n" , ret_ptr );
411411
412- // zero out the additional bytes of the newly allocated blocks
412+ // Zero out all the bytes of the newly allocated blocks.
413413 // This is needed because the blocks may have previously held pointers
414414 // to the heap and will not be set to something else if the caller
415415 // doesn't actually use the entire block. As such they will continue
416416 // to point to the heap and may prevent other blocks from being reclaimed.
417- memset ((byte * )ret_ptr + n_bytes , 0 , (end_block - start_block + 1 ) * BYTES_PER_BLOCK - n_bytes );
417+ memset ((byte * )ret_ptr , 0 , (end_block - start_block + 1 ) * BYTES_PER_BLOCK );
418418
419419 #if MICROPY_ENABLE_FINALISER
420420 if (has_finaliser ) {
@@ -620,8 +620,8 @@ void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
620620 ATB_FREE_TO_TAIL (bl );
621621 }
622622
623- // zero out the additional bytes of the newly allocated blocks (see comment above in gc_alloc)
624- memset ((byte * )ptr_in + n_bytes , 0 , new_blocks * BYTES_PER_BLOCK - n_bytes );
623+ // zero out the bytes of the newly allocated blocks (see comment above in gc_alloc)
624+ memset ((byte * )ptr_in + n_blocks * BYTES_PER_BLOCK , 0 , ( new_blocks - n_blocks ) * BYTES_PER_BLOCK );
625625
626626 #if EXTENSIVE_HEAP_PROFILING
627627 gc_dump_alloc_table ();
0 commit comments