aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-08-29 17:43:28 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-08-29 17:43:28 -0300
commita97f29f15487c5f584185f646f9cfc06a04b26ca (patch)
treee7ffd959fb80f9622d74a2efadfd7880037d64ca
parent4135f4f5862b57d9dbf3e9b7c6507f5c86516327 (diff)
downloadlua-a97f29f15487c5f584185f646f9cfc06a04b26ca.tar.gz
lua-a97f29f15487c5f584185f646f9cfc06a04b26ca.tar.bz2
lua-a97f29f15487c5f584185f646f9cfc06a04b26ca.zip
explicit stack control in the API
-rw-r--r--lapi.c6
-rw-r--r--lauxlib.c8
-rw-r--r--lauxlib.h5
-rw-r--r--ldo.c12
-rw-r--r--liolib.c3
-rw-r--r--lstrlib.c3
-rw-r--r--lua.h6
7 files changed, 27 insertions, 16 deletions
diff --git a/lapi.c b/lapi.c
index 5a000b49..e3d4a906 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.88 2000/08/28 17:57:04 roberto Exp roberto $ 2** $Id: lapi.c,v 1.89 2000/08/29 14:52:27 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -45,6 +45,10 @@ void luaA_pushobject (lua_State *L, const TObject *o) {
45 incr_top; 45 incr_top;
46} 46}
47 47
48int lua_stackspace (lua_State *L) {
49 return (L->stack_last - L->top);
50}
51
48 52
49 53
50/* 54/*
diff --git a/lauxlib.c b/lauxlib.c
index 12d1d067..2c54bd02 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.31 2000/08/28 17:57:04 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.32 2000/08/29 14:33:31 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -49,6 +49,12 @@ static void type_error (lua_State *L, int narg, const char *type_name) {
49} 49}
50 50
51 51
52void luaL_checkstack (lua_State *L, int space, const char *mes) {
53 if (space > lua_stackspace(L))
54 luaL_verror(L, "stack overflow (%.30s)", mes);
55}
56
57
52/* 58/*
53** use the 3rd letter of type names for testing: 59** use the 3rd letter of type names for testing:
54** nuMber, niL, stRing, fuNction, usErdata, taBle, anY 60** nuMber, niL, stRing, fuNction, usErdata, taBle, anY
diff --git a/lauxlib.h b/lauxlib.h
index 25dbc1f9..b57164d1 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.h,v 1.19 2000/08/09 19:16:57 roberto Exp roberto $ 2** $Id: lauxlib.h,v 1.20 2000/08/28 17:57:04 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -28,7 +28,8 @@ const char *luaL_opt_lstr (lua_State *L, int numArg, const char *def,
28double luaL_check_number (lua_State *L, int numArg); 28double luaL_check_number (lua_State *L, int numArg);
29double luaL_opt_number (lua_State *L, int numArg, double def); 29double luaL_opt_number (lua_State *L, int numArg, double def);
30 30
31void luaL_checktype(lua_State *L, int narg, const char *tname); 31void luaL_checkstack (lua_State *L, int space, const char *msg);
32void luaL_checktype (lua_State *L, int narg, const char *tname);
32 33
33void luaL_verror (lua_State *L, const char *fmt, ...); 34void luaL_verror (lua_State *L, const char *fmt, ...);
34int luaL_findstring (const char *name, const char *const list[]); 35int luaL_findstring (const char *name, const char *const list[]);
diff --git a/ldo.c b/ldo.c
index 907e62a6..79cf25a2 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.89 2000/08/29 14:57:23 roberto Exp roberto $ 2** $Id: ldo.c,v 1.90 2000/08/29 19:01:34 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -53,14 +53,8 @@ void luaD_checkstack (lua_State *L, int n) {
53 lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!"); 53 lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!");
54 } 54 }
55 else { 55 else {
56 lua_Debug dummy;
57 L->stack_last += EXTRA_STACK; /* to be used by error message */ 56 L->stack_last += EXTRA_STACK; /* to be used by error message */
58 if (lua_getstack(L, L->stacksize/SLOTS_PER_F, &dummy) == 0) { 57 lua_error(L, "stack overflow");
59 /* too few funcs on stack: doesn't look like a recursion loop */
60 lua_error(L, "Lua2C - C2Lua overflow");
61 }
62 else
63 lua_error(L, "stack overflow; possible recursion loop");
64 } 58 }
65 } 59 }
66} 60}
@@ -140,7 +134,7 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
140 StkId old_Cbase = L->Cbase; 134 StkId old_Cbase = L->Cbase;
141 int n; 135 int n;
142 L->Cbase = base; /* new base for C function */ 136 L->Cbase = base; /* new base for C function */
143 luaD_checkstack(L, nup); 137 luaD_checkstack(L, nup+LUA_MINSTACK); /* assures minimum stack size */
144 for (n=0; n<nup; n++) /* copy upvalues as extra arguments */ 138 for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
145 *(L->top++) = cl->upvalue[n]; 139 *(L->top++) = cl->upvalue[n];
146 n = (*cl->f.c)(L); /* do the actual call */ 140 n = (*cl->f.c)(L); /* do the actual call */
diff --git a/liolib.c b/liolib.c
index 4c0cbf2b..0a310de0 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.72 2000/08/28 17:57:04 roberto Exp roberto $ 2** $Id: liolib.c,v 1.73 2000/08/29 14:33:31 roberto Exp roberto $
3** Standard I/O (and system) library 3** Standard I/O (and system) library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -372,6 +372,7 @@ static int io_read (lua_State *L) {
372 firstarg = lastarg = 1; /* correct indices */ 372 firstarg = lastarg = 1; /* correct indices */
373 lua_pushstring(L, "*l"); /* push default argument */ 373 lua_pushstring(L, "*l"); /* push default argument */
374 } 374 }
375 luaL_checkstack(L, lastarg-firstarg+1, "too many results");
375 for (n = firstarg; n<=lastarg; n++) { 376 for (n = firstarg; n<=lastarg; n++) {
376 size_t l; 377 size_t l;
377 int success; 378 int success;
diff --git a/lstrlib.c b/lstrlib.c
index c380359b..0a0a6c13 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.46 2000/08/09 19:16:57 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.47 2000/08/28 17:57:04 roberto Exp roberto $
3** Standard library for string operations and pattern-matching 3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -400,6 +400,7 @@ static const char *lmemfind (const char *s1, size_t l1,
400 400
401static int push_captures (lua_State *L, struct Capture *cap) { 401static int push_captures (lua_State *L, struct Capture *cap) {
402 int i; 402 int i;
403 luaL_checkstack(L, cap->level, "too many captures");
403 for (i=0; i<cap->level; i++) { 404 for (i=0; i<cap->level; i++) {
404 int l = cap->capture[i].len; 405 int l = cap->capture[i].len;
405 if (l == -1) lua_error(L, "unfinished capture"); 406 if (l == -1) lua_error(L, "unfinished capture");
diff --git a/lua.h b/lua.h
index f9921a52..567f4e1a 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.60 2000/08/28 17:57:04 roberto Exp roberto $ 2** $Id: lua.h,v 1.61 2000/08/29 14:33:31 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension Language
4** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil 4** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
5** e-mail: lua@tecgraf.puc-rio.br 5** e-mail: lua@tecgraf.puc-rio.br
@@ -33,6 +33,9 @@
33#define LUA_MULTRET (-1) 33#define LUA_MULTRET (-1)
34 34
35 35
36#define LUA_MINSTACK 16
37
38
36/* error codes for lua_do* */ 39/* error codes for lua_do* */
37#define LUA_ERRFILE 2 40#define LUA_ERRFILE 2
38#define LUA_ERRSYNTAX 3 41#define LUA_ERRSYNTAX 3
@@ -58,6 +61,7 @@ void lua_close (lua_State *L);
58int lua_gettop (lua_State *L); 61int lua_gettop (lua_State *L);
59void lua_settop (lua_State *L, int index); 62void lua_settop (lua_State *L, int index);
60void lua_pushobject (lua_State *L, int index); 63void lua_pushobject (lua_State *L, int index);
64int lua_stackspace (lua_State *L);
61 65
62 66
63/* 67/*