浏览代码

explicit stack control in the API

Roberto Ierusalimschy 25 年之前
父节点
当前提交
a97f29f154
共有 7 个文件被更改,包括 27 次插入16 次删除
  1. 5 1
      lapi.c
  2. 7 1
      lauxlib.c
  3. 3 2
      lauxlib.h
  4. 3 9
      ldo.c
  5. 2 1
      liolib.c
  6. 2 1
      lstrlib.c
  7. 5 1
      lua.h

+ 5 - 1
lapi.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lapi.c,v 1.88 2000/08/28 17:57:04 roberto Exp roberto $
+** $Id: lapi.c,v 1.89 2000/08/29 14:52:27 roberto Exp roberto $
 ** Lua API
 ** Lua API
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -45,6 +45,10 @@ void luaA_pushobject (lua_State *L, const TObject *o) {
   incr_top;
   incr_top;
 }
 }
 
 
+int lua_stackspace (lua_State *L) {
+  return (L->stack_last - L->top);
+}
+
 
 
 
 
 /*
 /*

+ 7 - 1
lauxlib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lauxlib.c,v 1.31 2000/08/28 17:57:04 roberto Exp roberto $
+** $Id: lauxlib.c,v 1.32 2000/08/29 14:33:31 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -49,6 +49,12 @@ static void type_error (lua_State *L, int narg, const char *type_name) {
 }
 }
 
 
 
 
+void luaL_checkstack (lua_State *L, int space, const char *mes) {
+  if (space > lua_stackspace(L))
+    luaL_verror(L, "stack overflow (%.30s)", mes);
+}
+
+
 /*
 /*
 ** use the 3rd letter of type names for testing:
 ** use the 3rd letter of type names for testing:
 ** nuMber, niL, stRing, fuNction, usErdata, taBle, anY
 ** nuMber, niL, stRing, fuNction, usErdata, taBle, anY

+ 3 - 2
lauxlib.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lauxlib.h,v 1.19 2000/08/09 19:16:57 roberto Exp roberto $
+** $Id: lauxlib.h,v 1.20 2000/08/28 17:57:04 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -28,7 +28,8 @@ const char *luaL_opt_lstr (lua_State *L, int numArg, const char *def,
 double luaL_check_number (lua_State *L, int numArg);
 double luaL_check_number (lua_State *L, int numArg);
 double luaL_opt_number (lua_State *L, int numArg, double def);
 double luaL_opt_number (lua_State *L, int numArg, double def);
 
 
-void luaL_checktype(lua_State *L, int narg, const char *tname);
+void luaL_checkstack (lua_State *L, int space, const char *msg);
+void luaL_checktype (lua_State *L, int narg, const char *tname);
 
 
 void luaL_verror (lua_State *L, const char *fmt, ...);
 void luaL_verror (lua_State *L, const char *fmt, ...);
 int luaL_findstring (const char *name, const char *const list[]);
 int luaL_findstring (const char *name, const char *const list[]);

+ 3 - 9
ldo.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ldo.c,v 1.89 2000/08/29 14:57:23 roberto Exp roberto $
+** $Id: ldo.c,v 1.90 2000/08/29 19:01:34 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -53,14 +53,8 @@ void luaD_checkstack (lua_State *L, int n) {
       lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!");
       lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!");
     }
     }
     else {
     else {
-      lua_Debug dummy;
       L->stack_last += EXTRA_STACK;  /* to be used by error message */
       L->stack_last += EXTRA_STACK;  /* to be used by error message */
-      if (lua_getstack(L, L->stacksize/SLOTS_PER_F, &dummy) == 0) {
-        /* too few funcs on stack: doesn't look like a recursion loop */
-        lua_error(L, "Lua2C - C2Lua overflow");
-      }
-      else
-        lua_error(L, "stack overflow; possible recursion loop");
+      lua_error(L, "stack overflow");
     }
     }
   }
   }
 }
 }
@@ -140,7 +134,7 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
   StkId old_Cbase = L->Cbase;
   StkId old_Cbase = L->Cbase;
   int n;
   int n;
   L->Cbase = base;       /* new base for C function */
   L->Cbase = base;       /* new base for C function */
-  luaD_checkstack(L, nup);
+  luaD_checkstack(L, nup+LUA_MINSTACK);  /* assures minimum stack size */
   for (n=0; n<nup; n++)  /* copy upvalues as extra arguments */
   for (n=0; n<nup; n++)  /* copy upvalues as extra arguments */
     *(L->top++) = cl->upvalue[n];
     *(L->top++) = cl->upvalue[n];
   n = (*cl->f.c)(L);  /* do the actual call */
   n = (*cl->f.c)(L);  /* do the actual call */

+ 2 - 1
liolib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: liolib.c,v 1.72 2000/08/28 17:57:04 roberto Exp roberto $
+** $Id: liolib.c,v 1.73 2000/08/29 14:33:31 roberto Exp roberto $
 ** Standard I/O (and system) library
 ** Standard I/O (and system) library
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -372,6 +372,7 @@ static int io_read (lua_State *L) {
     firstarg = lastarg = 1;  /* correct indices */
     firstarg = lastarg = 1;  /* correct indices */
     lua_pushstring(L, "*l");  /* push default argument */
     lua_pushstring(L, "*l");  /* push default argument */
   }
   }
+  luaL_checkstack(L, lastarg-firstarg+1, "too many results");
   for (n = firstarg; n<=lastarg; n++) {
   for (n = firstarg; n<=lastarg; n++) {
     size_t l;
     size_t l;
     int success;
     int success;

+ 2 - 1
lstrlib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lstrlib.c,v 1.46 2000/08/09 19:16:57 roberto Exp roberto $
+** $Id: lstrlib.c,v 1.47 2000/08/28 17:57:04 roberto Exp roberto $
 ** Standard library for string operations and pattern-matching
 ** Standard library for string operations and pattern-matching
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -400,6 +400,7 @@ static const char *lmemfind (const char *s1, size_t l1,
 
 
 static int push_captures (lua_State *L, struct Capture *cap) {
 static int push_captures (lua_State *L, struct Capture *cap) {
   int i;
   int i;
+  luaL_checkstack(L, cap->level, "too many captures");
   for (i=0; i<cap->level; i++) {
   for (i=0; i<cap->level; i++) {
     int l = cap->capture[i].len;
     int l = cap->capture[i].len;
     if (l == -1) lua_error(L, "unfinished capture");
     if (l == -1) lua_error(L, "unfinished capture");

+ 5 - 1
lua.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lua.h,v 1.60 2000/08/28 17:57:04 roberto Exp roberto $
+** $Id: lua.h,v 1.61 2000/08/29 14:33:31 roberto Exp roberto $
 ** Lua - An Extensible Extension Language
 ** Lua - An Extensible Extension Language
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** e-mail: [email protected]
 ** e-mail: [email protected]
@@ -33,6 +33,9 @@
 #define LUA_MULTRET	(-1)
 #define LUA_MULTRET	(-1)
 
 
 
 
+#define LUA_MINSTACK	16
+
+
 /* error codes for lua_do* */
 /* error codes for lua_do* */
 #define LUA_ERRFILE	2
 #define LUA_ERRFILE	2
 #define LUA_ERRSYNTAX	3
 #define LUA_ERRSYNTAX	3
@@ -58,6 +61,7 @@ void           lua_close (lua_State *L);
 int            lua_gettop (lua_State *L);
 int            lua_gettop (lua_State *L);
 void           lua_settop (lua_State *L, int index);
 void           lua_settop (lua_State *L, int index);
 void           lua_pushobject (lua_State *L, int index);
 void           lua_pushobject (lua_State *L, int index);
+int            lua_stackspace (lua_State *L);
 
 
 
 
 /*
 /*