From 2b75950a8c6d822890f2ce5a53386410fe90b144 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 4 Sep 2009 12:46:20 +0200 Subject: [PATCH] Add some missing class type checks Some functions didn't check the class of objects they were passed but just casted them to the type they expected. This lead to code like e.g. the following to crash awesome: c.titlebar = c This adds a new function luaA_object_ref_class() which works like luaA_object_ref(), but which also checks the class of the object. Additionally, this function is now used in all necessary places. Signed-off-by: Uli Schlachter Signed-off-by: Julien Danjou --- common/luaobject.h | 14 ++++++++++++++ root.c | 2 +- tag.c | 4 ++-- titlebar.c | 2 +- wibox.c | 2 +- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/common/luaobject.h b/common/luaobject.h index 19aca17f..056368bd 100644 --- a/common/luaobject.h +++ b/common/luaobject.h @@ -106,6 +106,20 @@ luaA_object_ref(lua_State *L, int oud) return p; } +/** Reference an object and return a pointer to it checking its type. + * That only works with userdata. + * \param L The Lua VM state. + * \param oud The object index on the stack. + * \param class The class of object expected + * \return The object reference, or NULL if not referenceable. + */ +static inline void * +luaA_object_ref_class(lua_State *L, int oud, lua_class_t *class) +{ + luaA_checkudata(L, oud, class); + return luaA_object_ref(L, oud); +} + /** Unreference an object and return a pointer to it. * That only works with userdata, table, thread or function. * \param L The Lua VM state. diff --git a/root.c b/root.c index b91067a2..f9540ef5 100644 --- a/root.c +++ b/root.c @@ -126,7 +126,7 @@ luaA_root_keys(lua_State *L) lua_pushnil(L); while(lua_next(L, 1)) - key_array_append(&globalconf.keys, luaA_object_ref(L, -1)); + key_array_append(&globalconf.keys, luaA_object_ref_class(L, -1, &key_class)); int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection)); diff --git a/tag.c b/tag.c index dfb5365e..31357f90 100644 --- a/tag.c +++ b/tag.c @@ -127,7 +127,7 @@ tag_append_to_screen(lua_State *L, int udx, screen_t *s) int phys_screen = screen_virttophys(screen_index); tag->screen = s; - tag_array_append(&s->tags, luaA_object_ref(globalconf.L, udx)); + tag_array_append(&s->tags, luaA_object_ref_class(globalconf.L, udx, &tag_class)); ewmh_update_net_numbers_of_desktop(phys_screen); ewmh_update_net_desktop_names(phys_screen); ewmh_update_workarea(phys_screen); @@ -210,7 +210,7 @@ tag_client_emit_signal(lua_State *L, tag_t *t, client_t *c, const char *signame) void tag_client(client_t *c) { - tag_t *t = luaA_object_ref(globalconf.L, -1); + tag_t *t = luaA_object_ref_class(globalconf.L, -1, &tag_class); /* don't tag twice */ if(is_client_tagged(c, t)) diff --git a/titlebar.c b/titlebar.c index 2a363308..9d73efe3 100644 --- a/titlebar.c +++ b/titlebar.c @@ -224,7 +224,7 @@ void titlebar_client_attach(client_t *c) { /* check if we can register the object */ - wibox_t *t = luaA_object_ref(globalconf.L, -1); + wibox_t *t = luaA_object_ref_class(globalconf.L, -1, &wibox_class); titlebar_client_detach(c); diff --git a/wibox.c b/wibox.c index 3c51f664..feb8403d 100644 --- a/wibox.c +++ b/wibox.c @@ -738,7 +738,7 @@ wibox_attach(lua_State *L, int udx, screen_t *s) /* duplicate wibox */ lua_pushvalue(L, udx); /* ref it */ - wibox_t *wibox = luaA_object_ref(globalconf.L, -1); + wibox_t *wibox = luaA_object_ref_class(globalconf.L, -1, &wibox_class); wibox_detach(L, udx);