From c3e9a57a6fff08640f3ad05e8df971d5ddb37d51 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Thu, 18 Jun 2015 12:20:33 -0700 Subject: Fixed some bad bugs in upb_env. Also added a unit test for upb_encoder that demonstrates the bugs and the fix. --- upb/env.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'upb/env.c') diff --git a/upb/env.c b/upb/env.c index cc7a951..c42560a 100644 --- a/upb/env.c +++ b/upb/env.c @@ -68,6 +68,7 @@ static void *default_alloc(void *_ud, void *ptr, size_t oldsize, size_t size) { * updated to its new location. */ if (block->next) block->next->prev = block; if (block->prev) block->prev->next = block; + if (ud->head == from) ud->head = block; } } else { /* Insert at head of linked list. */ @@ -96,7 +97,7 @@ static void default_alloc_cleanup(void *_ud) { static bool default_err(void *ud, const upb_status *status) { UPB_UNUSED(ud); - fprintf(stderr, "upb error: %s\n", upb_status_errmsg(status)); + UPB_UNUSED(status); return false; } @@ -217,7 +218,6 @@ static size_t align_up(size_t size) { UPB_FORCEINLINE static void *seeded_alloc(void *ud, void *ptr, size_t oldsize, size_t size) { upb_seededalloc *a = ud; - UPB_UNUSED(ptr); size = align_up(size); @@ -235,7 +235,11 @@ UPB_FORCEINLINE static void *seeded_alloc(void *ud, void *ptr, size_t oldsize, /* Is `ptr` part of the user-provided initial block? Don't pass it to the * default allocator if so; otherwise, it may try to realloc() the block. */ if (chptr >= a->mem_base && chptr < a->mem_limit) { - return a->alloc(a->alloc_ud, NULL, 0, size); + void *ret; + assert(chptr + oldsize <= a->mem_limit); + ret = a->alloc(a->alloc_ud, NULL, 0, size); + if (ret) memcpy(ret, ptr, oldsize); + return ret; } else { return a->alloc(a->alloc_ud, ptr, oldsize, size); } -- cgit v1.2.3