lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




2010/11/22 Roberto Ierusalimschy <roberto@inf.puc-rio.br>
> I obtain a segfault with the following script :
>
> local u = io.tmpfile()
> local t = {}
> local r = debug.setuservalue(u, t)
> assert(r == u)
> assert(debug.getuservalue(u) == t)
> print 'ok'
> -- segfault during the finalization of u

About the debug library (manual 6.10):

 This library provides the functionality of the debug interface to Lua
 programs.  You should exert care when using this library.  Several of
 these functions violate basic assumptions about Lua code (e.g., [...];
 that Lua programs do not crash) [...]

(In particular, you should not change the uservalue of a file handle.)


I know this script does a stupid thing.
I expected more robustness.
That could be achieved with :

diff --git a/src/liolib.c b/src/liolib.c
--- a/src/liolib.c
+++ b/src/liolib.c
@@ -167,6 +167,8 @@ static int io_fclose (lua_State *L) {
 static int aux_close (lua_State *L) {
   lua_getuservalue(L, 1);
   lua_getfield(L, -1, "__close");
+  if (lua_isnil(L, -1))
+    return 0;
   return (lua_tocfunction(L, -1))(L);
 }
 
François

 
-- Roberto