Re: Bug in luaL_newmetatable: undersized table hint causes permanent corruption under OOM

Roberto Ierusalimschy <[email protected]> Fri, 12 Jun 2026 11:51:19 -0300
Newsgroups gmane.comp.lang.lua.general
Message-ID <[email protected]>
> I found a bug in luaL_newmetatable (lauxlib.c, line 318 on current master)
> where the table creation hint of 2 is too small, causing an incomplete
> metatable to persist in the registry after a memory allocation failure.
> 
> [...]
> Fix:
> 
> --- a/lauxlib.c
> +++ b/lauxlib.c
> @@ -318,7 +318,7 @@
>    if (luaL_getmetatable(L, tname) != LUA_TNIL)
>      return 0;
>    lua_pop(L, 1);
> -  lua_createtable(L, 0, 2);  /* create metatable */
> +  lua_createtable(L, 0, 4); /* create metatable (room for __name + methods) */
>    lua_pushstring(L, tname);
>    lua_setfield(L, -2, "__name");
>    lua_pushvalue(L, -1);
> 
> Changing the hint from 2 to 4 gives the metatable enough hash slots to
> accommodate __name plus the typical 2-3 metamethods without triggering a
> rehash, eliminating the failure window.

The problem diagnosis is correct, but this fix is not. First, the
metatable may need more than 2 extra slots. Second, the creation of
the keys (strings) also can trigger allocation errors.

-- Roberto

-- 
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/20260612145119.GA251126%40arraial.inf.puc-rio.br.