diff options
author | Josh Haberman <jhaberman@gmail.com> | 2015-06-18 12:20:33 -0700 |
---|---|---|
committer | Josh Haberman <jhaberman@gmail.com> | 2015-06-22 00:53:04 -0700 |
commit | c3e9a57a6fff08640f3ad05e8df971d5ddb37d51 (patch) | |
tree | 0bca6f63eb1813dd3f21b4e570f7f586e829f519 /upb/env.c | |
parent | 19a973a85ef5a2eaf1ca59d09ec2d6e972c0cd24 (diff) |
Fixed some bad bugs in upb_env.
Also added a unit test for upb_encoder that
demonstrates the bugs and the fix.
Diffstat (limited to 'upb/env.c')
-rw-r--r-- | upb/env.c | 10 |
1 files changed, 7 insertions, 3 deletions
@@ -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); } |