fix: error pointer in realloc

This commit is contained in:
Renge 2022-03-25 23:26:21 -04:00
parent 908663e721
commit 2b554b4c5c
2 changed files with 8 additions and 18 deletions

View File

@ -4,23 +4,13 @@
int main(int argc, char const *argv[]) { int main(int argc, char const *argv[]) {
sf_set_magic(0x0); sf_set_magic(0x0);
size_t sz_u = 200, sz_v = 150, sz_w = 50, sz_x = 150, sz_y = 200, sz_z = 250; size_t sz_x = sizeof(int), sz_y = 10, sz_x1 = sizeof(int) * 20;
void *u = sf_malloc(sz_u); void *x = sf_malloc(sz_x);
/* void *v = */ sf_malloc(sz_v); /* void *y = */ sf_malloc(sz_y);
void *w = sf_malloc(sz_w); x = sf_realloc(x, sz_x1);
/* void *x = */ sf_malloc(sz_x);
void *y = sf_malloc(sz_y);
/* void *z = */ sf_malloc(sz_z);
sf_free(u); // cr_assert_not_null(x, "x is NULL!");
sf_free(w); // sf_block *bp = (sf_block *)((char *)x - 16);
sf_free(y);
int i = 3;
sf_block *bp = sf_free_list_heads[i].body.links.next;
printf("Wrong first block in free list %d: (found=%p, exp=%p, u=%p, w=%p)\n",
i, &bp->header, (char *)y - 8, (char *)u - 8, (char *)w - 8);
sf_show_heap();
sf_show_heap(); sf_show_heap();

View File

@ -170,11 +170,11 @@ void *sf_realloc(void *pp, sf_size_t rsize)
sf_size_t new_size = get_min_size(size); sf_size_t new_size = get_min_size(size);
if (new_size >= size) if (new_size >= size)
{ {
sf_block *new = sf_malloc(rsize); sf_block *new = (sf_block *)(((intptr_t)sf_malloc(rsize)) - 2 * sizeof(sf_header));
if (new == NULL) if (new == NULL)
return NULL; return NULL;
memcpy(new->body.payload, block->body.payload, size); memcpy(new->body.payload, block->body.payload, size);
sf_free(block); sf_free(pp);
return new->body.payload; return new->body.payload;
} }
else else