From 22a5137299878887d547830815f372edd317106c Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 11 Dec 2010 13:25:44 +0100 Subject: [PATCH] Print a warning if an invalid reference is dropped (FS#820) Previously, if you called luaA_object_decref() it would silently *create* a new reference with reference count -1. Obviously, this is not good. Signed-off-by: Uli Schlachter --- common/luaobject.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/common/luaobject.c b/common/luaobject.c index 46450a2d..5dde2096 100644 --- a/common/luaobject.c +++ b/common/luaobject.c @@ -20,6 +20,7 @@ */ #include "common/luaobject.h" +#include "common/backtrace.h" /** Setup the object system at startup. * \param L The Lua VM state. @@ -113,6 +114,17 @@ luaA_object_decref(lua_State *L, int tud, void *pointer) lua_rawget(L, -2); /* Get the number of references and decrement it */ int count = lua_tonumber(L, -1) - 1; + /* Did we find the item in our table? (tonumber(nil)-1) is -1 */ + if (count < 0) + { + buffer_t buf; + backtrace_get(&buf); + warn("BUG: Reference not found: %d %p\n%s", tud, pointer, buf.s); + + /* Pop reference count and metatable */ + lua_pop(L, 2); + return; + } lua_pop(L, 1); /* Push the pointer (key) */ lua_pushlightuserdata(L, pointer);