Re: Similar issue with 'luaL_newmetatable' in I/O standard library

Roberto Ierusalimschy <[email protected]> Tue, 21 Jul 2026 09:22:57 -0300
Newsgroups gmane.comp.lang.lua.general
Message-ID <[email protected]>
> In regards to commit 'bc4bbce' that fixes the issue with 'luaL_newmetatable'
> in auxiliary library.
> 
> The creation of *file* metatable in Input/output standard library faces the
> same
> problem in case of OOM.
> 
> liolib.c:797 the function 'createmeta'
> 
> static void createmeta (lua_State *L) {
>   luaL_newmetatable(L, LUA_FILEHANDLE);  /* metatable for file handles */
>   luaL_setfuncs(L, metameth, 0);  /* add metamethods to new metatable */
>   luaL_newlibtable(L, meth);  /* create method table */
>   luaL_setfuncs(L, meth, 0);  /* add file methods to method table */
>   lua_setfield(L, -2, "__index");  /* metatable.__index = method table */
>   lua_pop(L, 1);  /* pop metatable */
> }
> 
> (After 'luaL_newmetatable' the table is already in the registry; the proper
> re-'require'
> of I/O library skips the creation of the file metatable.)

I don't see the problem here. If the table is already in the registry,
it doesn't need to be created again. In case of a re-'require', the
initialization (luaL_setfuncs) will be tried again, and the require will
only complete if that initialization goes without errors.

The problematic uses seem to be only when the result of luaL_newmetatable
is used to conditionally initialize the metatable.

-- 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/20260721122257.GA58175%40arraial.inf.puc-rio.br.