server: Safe cast a "wl_object *" to "wl_resource *"

Client message observers 5/6

When given an array of wl_arguments for a wl_closure, the .o field is an
opaque wl_object pointer, which the server implementation cannot really do
anything with, without a potentially unsafe cast that assumes details about the
internal implementation.

By adding a wl_resource_from_object() function to the client interface, the client
can safely get the wl_resource pointer.

This can be used by server protocol loggers in particular to get the resource id
and class name, for logging those details

Signed-off-by: Lloyd Pique <lpique@google.com>
This commit is contained in:
Lloyd Pique 2022-03-11 19:10:07 -08:00
parent e5e7cc57fa
commit 7698137663
2 changed files with 25 additions and 0 deletions

View file

@ -628,6 +628,9 @@ struct wl_listener *
wl_resource_get_destroy_listener(struct wl_resource *resource,
wl_notify_func_t notify);
struct wl_resource *
wl_resource_from_object(struct wl_object *object);
#define wl_resource_for_each(resource, list) \
for (resource = 0, resource = wl_resource_from_link((list)->next); \
wl_resource_get_link(resource) != (list); \

View file

@ -905,6 +905,28 @@ wl_resource_get_class(struct wl_resource *resource)
return resource->object.interface->name;
}
/** Safely converts an object into its corresponding resource
*
* \param object object to get the resource for
* \return A corresponding resource, or NULL on failure
*
* Safely converts an object into its corresponding resource.
*
* This is useful for implementing functions that are given a \c wl_argument
* array, and that need to do further introspection on the ".o" field, as it
* is otherwise an opaque type.
*
* \memberof wl_resource
*/
WL_EXPORT struct wl_resource *
wl_resource_from_object(struct wl_object *object)
{
struct wl_resource *resource;
if (object == NULL)
return NULL;
return wl_container_of(object, resource, object);
}
/**
* Add a listener to be called at the beginning of wl_client destruction
*