mirror of
https://github.com/lua/lua.git
synced 2026-07-22 14:09:08 +00:00
Bug: 'luaL_newmetatable' used in a wrong way
The call to 'luaL_newmetatable' in 'newbox' can leave an incomplete metatable in the registry, if 'luaL_setfuncs' raises a memory error.
This commit is contained in:
17
lauxlib.c
17
lauxlib.c
@@ -513,12 +513,25 @@ static const luaL_Reg boxmt[] = { /* box metamethods */
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** Get/create metatable (MT) for boxes
|
||||
*/
|
||||
static void getBoxMT (lua_State *L) {
|
||||
const char *BOXMT = "_UBOX*"; /* key for the metatable */
|
||||
if (luaL_getmetatable(L, BOXMT) == LUA_TNIL) { /* MT not created yet? */
|
||||
luaL_newlibtable(L, boxmt); /* create it */
|
||||
luaL_setfuncs(L, boxmt, 0); /* initialize it */
|
||||
lua_copy(L, -1, -2); /* change stack from nil,MT to MT,MT */
|
||||
lua_setfield(L, LUA_REGISTRYINDEX, BOXMT); /* store MT in the registry */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void newbox (lua_State *L) {
|
||||
UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0);
|
||||
box->box = NULL;
|
||||
box->bsize = 0;
|
||||
if (luaL_newmetatable(L, "_UBOX*")) /* creating metatable? */
|
||||
luaL_setfuncs(L, boxmt, 0); /* set its metamethods */
|
||||
getBoxMT(L);
|
||||
lua_setmetatable(L, -2);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user