From f8543464c13584e39bfacee694ee1ed80ac121f4 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 26 May 2015 23:58:32 +0800 Subject: fix a severe bug in memory management of userdata --- io/init.lua | 3 ++- io/param.c | 10 ++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'io') diff --git a/io/init.lua b/io/init.lua index d96f062..dc1c6c3 100644 --- a/io/init.lua +++ b/io/init.lua @@ -22,7 +22,8 @@ function nerv.ParamFile:read_param(id) if metadata == nil then nerv_error("param with id %s does not exist", id) end - local param = assert(loadstring("return " .. metadata.type .. "(" .. id .. ")"))() + local param = assert(loadstring("return " .. + metadata.type .. "(\"" .. id .. "\")"))() param:set_info(metadata.info) param:read(self:get_chunkdata(id)) return param diff --git a/io/param.c b/io/param.c index 477df28..a57b183 100644 --- a/io/param.c +++ b/io/param.c @@ -77,7 +77,8 @@ ParamChunkData *get_param_chunk_data(FILE *fp, ParamChunkInfo *info) { pcd->data = (char *)malloc(info->length); pcd->fp = fmemopen(pcd->data, info->length, "r"); assert(fseeko(fp, info->offset, SEEK_SET) == 0); - assert(fread(pcd->data, 1, info->length, fp) == (size_t)info->length); + if (fread(pcd->data, 1, info->length, fp) != (size_t)info->length) + return NULL; return pcd; } @@ -239,6 +240,7 @@ int nerv_param_file_write_chunkdata(lua_State *L) { int nerv_param_file_get_chunkdata(lua_State *L) { ParamFileHandle *pfh; ParamChunkInfo *pci; + ParamChunkData *pcd; const char *id = luaL_checkstring(L, 2); lua_getfield(L, 1, "handle"); @@ -252,9 +254,9 @@ int nerv_param_file_get_chunkdata(lua_State *L) { return 0; lua_getfield(L, -1, "chunk"); pci = luaT_checkudata(L, -1, nerv_param_chunk_info_tname); - - luaT_pushudata(L, get_param_chunk_data(pfh->fp, pci), - nerv_param_chunk_data_tname); + if (!(pcd = get_param_chunk_data(pfh->fp, pci))) + nerv_error(L, "unexpected end of file"); + luaT_pushudata(L, pcd, nerv_param_chunk_data_tname); return 1; } -- cgit v1.2.3-70-g09d2