瀏覽代碼

global_State must be deallocated (and so allocated) with NULL also
(otherwise it trys to decrement inside itself after its own free)

Roberto Ierusalimschy 23 年之前
父節點
當前提交
d404f0c276
共有 2 個文件被更改,包括 8 次插入5 次删除
  1. 4 2
      lmem.c
  2. 4 3
      lstate.c

+ 4 - 2
lmem.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lmem.c,v 1.58 2002/10/08 18:45:07 roberto Exp roberto $
+** $Id: lmem.c,v 1.59 2002/10/25 21:29:20 roberto Exp roberto $
 ** Interface to Memory Manager
 ** See Copyright Notice in lua.h
 */
@@ -67,6 +67,7 @@ void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) {
       l_free(block, oldsize);
       block = NULL;
     }
+    else return NULL;  /* avoid `nblocks' computations when oldsize==size==0 */
   }
   else if (size >= MAX_SIZET)
     luaG_runerror(L, "memory allocation error: block too big");
@@ -78,7 +79,8 @@ void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) {
       else return NULL;  /* error before creating state! */
     }
   }
-  if (L && G(L)) {
+  if (L) {
+    lua_assert(G(L) != NULL && G(L)->nblocks > 0);
     G(L)->nblocks -= oldsize;
     G(L)->nblocks += size;
   }

+ 4 - 3
lstate.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.c,v 1.112 2002/11/18 11:01:55 roberto Exp roberto $
+** $Id: lstate.c,v 1.113 2002/11/19 14:12:13 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -84,8 +84,9 @@ static void freestack (lua_State *L, lua_State *L1) {
 */
 static void f_luaopen (lua_State *L, void *ud) {
   /* create a new global state */
-  global_State *g = luaM_new(L, global_State);
+  global_State *g = luaM_new(NULL, global_State);
   UNUSED(ud);
+  if (g == NULL) luaD_throw(L, LUA_ERRMEM);
   L->l_G = g;
   g->mainthread = L;
   g->GCthreshold = 0;  /* mark it as unfinished state */
@@ -147,7 +148,7 @@ static void close_state (lua_State *L) {
   freestack(L, L);
   if (G(L)) {
     lua_assert(G(L)->nblocks == sizeof(lua_State) + sizeof(global_State));
-    luaM_freelem(L, G(L));
+    luaM_freelem(NULL, G(L));
   }
   freestate(NULL, L);
 }