From 838009ba2b8ea1e99061c66e0fbd9cb53a96ec20 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Fri, 8 May 2015 17:20:55 -0700 Subject: Fixes for the open-source build. --- upb/bindings/lua/upb.c | 2 +- upb/bindings/lua/upb/pb.c | 15 +++------------ upb/env.c | 8 +++++--- upb/env.h | 6 +++--- upb/pb/decoder.c | 4 +++- 5 files changed, 15 insertions(+), 20 deletions(-) (limited to 'upb') diff --git a/upb/bindings/lua/upb.c b/upb/bindings/lua/upb.c index 5ad0235..b35af24 100644 --- a/upb/bindings/lua/upb.c +++ b/upb/bindings/lua/upb.c @@ -1358,7 +1358,7 @@ static size_t align_up(size_t val, size_t align) { // If we always read/write as a consistent type to each value, this shouldn't // violate aliasing. -#define DEREF(msg, ofs, type) *(type*)(&msg->data[ofs]) +#define DEREF(msg, ofs, type) *(type*)((char*)msg + sizeof(lupb_msg) + ofs) lupb_msg *lupb_msg_check(lua_State *L, int narg) { lupb_msg *msg = luaL_checkudata(L, narg, LUPB_MSG); diff --git a/upb/bindings/lua/upb/pb.c b/upb/bindings/lua/upb/pb.c index ea3d755..920648f 100644 --- a/upb/bindings/lua/upb/pb.c +++ b/upb/bindings/lua/upb/pb.c @@ -41,13 +41,6 @@ static int lupb_pbdecodermethod_new(lua_State *L) { return 1; // The DecoderMethod wrapper. } -// We implement upb's allocation function by allocating a Lua userdata. -// This is a raw hunk of memory that will be GC'd by Lua. -static void *lua_alloc(void *ud, size_t size) { - lua_State *L = ud; - return lua_newuserdata(L, size); -} - // Unlike most of our exposed Lua functions, this does not correspond to an // actual method on the underlying DecoderMethod. But it's convenient, and // important to implement in C because we can do stack allocation and @@ -77,11 +70,9 @@ static int lupb_pbdecodermethod_parse(lua_State *L) { upb_pbdecoder *decoder = upb_pbdecoder_create(&env, method, &sink); upb_bufsrc_putbuf(pb, len, upb_pbdecoder_input(decoder)); - // This won't get called in the error case, which longjmp's across us. But - // since we made our alloc function allocate only GC-able memory, that - // shouldn't matter. It *would* matter if the environment had references to - // any non-memory resources (ie. filehandles). As an alternative to this we - // could make the environment itself a userdata. + // TODO: This won't get called in the error case, which longjmp's across us. + // This will cause the memory to leak. To remedy this, we should make the + // upb_env wrapped in a userdata that guarantees this will get called. upb_env_uninit(&env); lupb_checkstatus(L, &status); diff --git a/upb/env.c b/upb/env.c index ae5fb85..7fa3334 100644 --- a/upb/env.c +++ b/upb/env.c @@ -43,7 +43,7 @@ static void *default_alloc(void *_ud, void *ptr, size_t oldsize, size_t size) { UPB_UNUSED(oldsize); default_alloc_ud *ud = _ud; - mem_block *from = ptr ? ptr - sizeof(mem_block) : NULL; + mem_block *from = ptr ? (void*)((char*)ptr - sizeof(mem_block)) : NULL; #ifndef NDEBUG if (from) { @@ -214,7 +214,9 @@ UPB_FORCEINLINE static void *seeded_alloc(void *ud, void *ptr, size_t oldsize, upb_seededalloc *a = ud; size = align_up(size); - if (oldsize == 0 && size <= a->mem_limit - a->mem_ptr) { + assert(a->mem_limit >= a->mem_ptr); + + if (oldsize == 0 && size <= (size_t)(a->mem_limit - a->mem_ptr)) { // Fast path: we can satisfy from the initial allocation. void *ret = a->mem_ptr; a->mem_ptr += size; @@ -229,7 +231,7 @@ UPB_FORCEINLINE static void *seeded_alloc(void *ud, void *ptr, size_t oldsize, void upb_seededalloc_init(upb_seededalloc *a, void *mem, size_t len) { a->mem_base = mem; a->mem_ptr = mem; - a->mem_limit = mem + len; + a->mem_limit = (char*)mem + len; a->need_cleanup = false; a->returned_allocfunc = false; diff --git a/upb/env.h b/upb/env.h index 500b0ac..78dda20 100644 --- a/upb/env.h +++ b/upb/env.h @@ -177,9 +177,9 @@ UPB_DEFINE_STRUCT0(upb_seededalloc, void *default_alloc_ud; // Pointers for the initial memory region. - void *mem_base; - void *mem_ptr; - void *mem_limit; + char *mem_base; + char *mem_ptr; + char *mem_limit; // For future expansion, since the size of this struct is exposed to users. void *future1; diff --git a/upb/pb/decoder.c b/upb/pb/decoder.c index f23645d..a780666 100644 --- a/upb/pb/decoder.c +++ b/upb/pb/decoder.c @@ -966,7 +966,9 @@ size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d) { } bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max) { - if (max < d->top - d->stack) { + assert(d->top >= d->stack); + + if (max < (size_t)(d->top - d->stack)) { // Can't set a limit smaller than what we are currently at. return false; } -- cgit v1.2.3