diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-07-04 16:40:18 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-07-04 16:40:18 -0300 |
commit | bfcf06d91a87b7ffb8c83e290db0cb6176a167f8 (patch) | |
tree | 0bcba905a2772e536c845e39e9eeed0c7330312c /ltests.c | |
parent | 0280407fc54f9b6225139c5ac27326f98f0cf043 (diff) | |
download | lua-github-bfcf06d91a87b7ffb8c83e290db0cb6176a167f8.tar.gz |
Avoid memory allocation in some functions from 'ltests.c'
To allow their use in memory tests, some functions in 'ltests.c'
should never allocate memory. To avoid this allocation, the
library registers the strings used for status codes, and keeps
the variable '_WARN' always defined (with false instead of nil).
Diffstat (limited to 'ltests.c')
-rw-r--r-- | ltests.c | 27 |
1 files changed, 20 insertions, 7 deletions
@@ -121,7 +121,8 @@ static void warnf (void *ud, const char *msg, int tocont) { strcat(buff, msg); /* add new message to current warning */ if (!tocont) { /* message finished? */ lua_unlock(L); - if (lua_getglobal(L, "_WARN") == LUA_TNIL) + lua_getglobal(L, "_WARN"); + if (!lua_toboolean(L, -1)) lua_pop(L, 1); /* ok, no previous unexpected warning */ else { badexit("Unhandled warning in store mode: %s\naborting...\n", @@ -1282,10 +1283,19 @@ static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) { } -static void pushcode (lua_State *L, int code) { - static const char *const codes[] = {"OK", "YIELD", "ERRRUN", - "ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"}; - lua_pushstring(L, codes[code]); +static const char *const statcodes[] = {"OK", "YIELD", "ERRRUN", + "ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"}; + +/* +** Avoid these stat codes from being collected, to avoid possible +** memory error when pushing them. +*/ +static void regcodes (lua_State *L) { + unsigned int i; + for (i = 0; i < sizeof(statcodes) / sizeof(statcodes[0]); i++) { + lua_pushboolean(L, 1); + lua_setfield(L, LUA_REGISTRYINDEX, statcodes[i]); + } } @@ -1508,7 +1518,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { lua_pushnumber(L1, (lua_Number)getnum); } else if EQ("pushstatus") { - pushcode(L1, status); + lua_pushstring(L1, statcodes[status]); } else if EQ("pushstring") { lua_pushstring(L1, getstring); @@ -1710,7 +1720,7 @@ static int Cfunc (lua_State *L) { static int Cfunck (lua_State *L, int status, lua_KContext ctx) { - pushcode(L, status); + lua_pushstring(L, statcodes[status]); lua_setglobal(L, "status"); lua_pushinteger(L, ctx); lua_setglobal(L, "ctx"); @@ -1865,6 +1875,9 @@ int luaB_opentests (lua_State *L) { void *ud; lua_atpanic(L, &tpanic); lua_setwarnf(L, &warnf, L); + lua_pushboolean(L, 0); + lua_setglobal(L, "_WARN"); /* _WARN = false */ + regcodes(L); atexit(checkfinalmem); lua_assert(lua_getallocf(L, &ud) == debug_realloc); lua_assert(ud == cast_voidp(&l_memcontrol)); |