awesome/common
Uli Schlachter 1408e8b952 Unset object's metatable in __gc
Run the following code:

  do
      local d
      local f = function() d.visible = true end
      if _VERSION >= "Lua 5.2" then
          setmetatable({}, { __gc = f })
      else
          getmetatable(newproxy(true)).__gc = f
      end
      d = drawin({})
  end
  collectgarbage("collect")

Awesome will segfault.

The reason for this is that after the above code ran, all variables in it are
unreferenced and will be garbage-collected at the next sweep phase. Lua runs
garbage collectors in the inverse order that their corresponding objects were
"marked" which means for the above code that the drawin's garbage collector will
run before function f runs. So the code will access the drawin after its
destructor already ran. Obviously, awesome's C code does not expect nor
correctly deal with this situation and was dereferencing a NULL pointer.

To fix this, this commit "unsets" the metatable of a userdata object when it is
being garbage collected. Since the type of a userdata is inferred via its
metatable, the object will no longer be accepted by luaA_toudata().

For the above code this will result in an unhelpful error message saying that
something tried to index a userdata, but userdata cannot be indexed. At least we
no longer crash and the traceback of the error will hopefully point at some __gc
metamethod which should be enough of a hint to figure out the problem.

Thanks-to: http://blog.reverberate.org/2014/06/beware-of-lua-finalizers-in-c-modules.html
Signed-off-by: Uli Schlachter <psychon@znc.in>
2015-06-20 12:33:17 +02:00
..
array.h Make objects properly inherit signals from classes 2014-03-07 14:42:03 +01:00
atoms.c remove encoding=utf-8 from modelines 2011-09-11 17:34:09 +02:00
atoms.h remove encoding=utf-8 from modelines 2011-09-11 17:34:09 +02:00
atoms.list Revert "client: Add c.blob property" 2014-03-07 16:21:11 +01:00
backtrace.c remove encoding=utf-8 from modelines 2011-09-11 17:34:09 +02:00
backtrace.h remove encoding=utf-8 from modelines 2011-09-11 17:34:09 +02:00
buffer.c Clean up header includes 2014-03-30 20:07:48 +02:00
buffer.h Grammar/spelling corrections in comments and docs. 2009-08-31 10:39:36 +02:00
luaclass.c Unset object's metatable in __gc 2015-06-20 12:33:17 +02:00
luaclass.h client: Include c.name in the result of tostring(c) 2014-12-06 18:16:05 +01:00
lualib.c Clean up header includes 2014-03-30 20:07:48 +02:00
lualib.h Clean up header includes 2014-03-30 20:07:48 +02:00
luaobject.c luaA_object_emit_signal: check for valid object 2015-02-10 14:52:47 +01:00
luaobject.h Make objects properly inherit signals from classes 2014-03-07 14:42:03 +01:00
signal.h Clean up header includes 2014-03-30 20:07:48 +02:00
util.c Clean up header includes 2014-03-30 20:07:48 +02:00
util.h common/util.h: dodgy non-__GNUC__ p_delete 2012-07-31 16:33:05 +02:00
version.c Only include awesome-version-internal.h in one place 2014-04-13 17:48:03 +02:00
version.h Only include awesome-version-internal.h in one place 2014-04-13 17:48:03 +02:00
xcursor.c Switch from libXcursor to libxcb-cursor 2013-09-19 16:48:10 +02:00
xcursor.h Switch from libXcursor to libxcb-cursor 2013-09-19 16:48:10 +02:00
xembed.c Clean up header includes 2014-03-30 20:07:48 +02:00
xembed.h Clean up header includes 2014-03-30 20:07:48 +02:00
xutil.c Clean up header includes 2014-03-30 20:07:48 +02:00
xutil.h Clean up header includes 2014-03-30 20:07:48 +02:00