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 <psychon@znc.in>
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Uli Schlachter 2009-09-04 12:46:20 +02:00 committed by Julien Danjou
parent fa89775626
commit 2b75950a8c
5 changed files with 19 additions and 5 deletions

View File

@ -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.

2
root.c
View File

@ -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));

4
tag.c
View File

@ -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))

View File

@ -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);

View File

@ -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);