diff options
author | Joshua Haberman <jhaberman@gmail.com> | 2015-05-15 11:14:27 -0700 |
---|---|---|
committer | Joshua Haberman <jhaberman@gmail.com> | 2015-05-15 11:14:27 -0700 |
commit | 93791bfe65d210385ab1fb1eb1ccd77fca18b2bf (patch) | |
tree | bb6d5a90f78e1f19063ba43a0aa3f970b3265252 /upb | |
parent | 37cffddc5d4a9fd9eb7eaff4e1c843d379ad408b (diff) | |
parent | 710111cc7c58f9b51be949fb9a30487372af5dd9 (diff) |
Merge pull request #19 from cfallin/upb-seededalloc-fix
Bugfix: seeded_alloc() should not realloc() user-provided memory.
Diffstat (limited to 'upb')
-rw-r--r-- | upb/env.c | 11 |
1 files changed, 10 insertions, 1 deletions
@@ -211,6 +211,8 @@ 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_UNUSED(ptr); + upb_seededalloc *a = ud; size = align_up(size); @@ -224,7 +226,14 @@ UPB_FORCEINLINE static void *seeded_alloc(void *ud, void *ptr, size_t oldsize, } else { // Slow path: fallback to other allocator. a->need_cleanup = true; - return a->alloc(a->alloc_ud, ptr, oldsize, size); + // 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. + char *chptr = ptr; + if (chptr >= a->mem_base && chptr < a->mem_limit) { + return a->alloc(a->alloc_ud, NULL, 0, size); + } else { + return a->alloc(a->alloc_ud, ptr, oldsize, size); + } } } |