diff options
author | Determinant <[email protected]> | 2015-05-26 23:58:32 +0800 |
---|---|---|
committer | Determinant <[email protected]> | 2015-05-26 23:58:32 +0800 |
commit | f8543464c13584e39bfacee694ee1ed80ac121f4 (patch) | |
tree | 3e29ffd5205659fbf3f908b5406522e4bab1c2e9 /io/param.c | |
parent | 910640c0ef7c43d586180241f79723973e0e7d35 (diff) |
fix a severe bug in memory management of userdata
Diffstat (limited to 'io/param.c')
-rw-r--r-- | io/param.c | 10 |
1 files changed, 6 insertions, 4 deletions
@@ -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; } |