Skip to content

Commit

Permalink
struct: fix memory leak in buffer.pull()
Browse files Browse the repository at this point in the history
Do not increase the refcount when returning the pulled buffer contents
as string since the returned value already is the sole reference.
Without this change, pulled buffer contents will be leaked whenever
the `pull()` function is used.

Also ensure that the buffer memory is completely zero initialized when
it is allocated from scratch, the existing logic only cleared the trailing
data area on reallocations but never the head on fresh allocations.

Signed-off-by: Jo-Philipp Wich <[email protected]>
  • Loading branch information
jow- committed Dec 30, 2024
1 parent 2f7081a commit d813436
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -2514,7 +2514,10 @@ grow_buffer(uc_vm_t *vm, void **buf, size_t *bufsz, size_t length)
return false;
}

memset(tmp + overhead + old_size - 1, 0, new_size - old_size + 1);
if (*buf)
memset(tmp + overhead + old_size - 1, 0, new_size - old_size + 1);
else
memset(tmp, 0, new_size + overhead);

*buf = tmp;
*bufsz = new_size;
Expand Down Expand Up @@ -3655,7 +3658,7 @@ uc_fmtbuf_pull(uc_vm_t *vm, size_t nargs)
buffer->position = 0;
buffer->length = 0;

return ucv_get(&us->header);
return &us->header;
}


Expand Down

0 comments on commit d813436

Please sign in to comment.