[RFC wayland 03/12] wayland-util: return -1 on OOM in wl_map_insert_new()

Kristian Høgsberg hoegsberg at gmail.com
Mon Oct 15 12:12:34 PDT 2012


On Thu, Oct 11, 2012 at 11:37:44PM +0200, David Herrmann wrote:
> If we cannot increase the array for new entries, we now return -1 instead
> of accessing invalid memory.

We can't change the data type of IDs, it has to stay uint32_t.  What
we can do instead is do define an invalid ID.  Either use 0 (and
special cases the initialization case where we insert NULL as ID 0) or
use 0xffffffff.  Server allocated IDs are in the 0xff000000 -
0xffffffff range, so we do use IDs that would be negative as int32_ts.

Kristian


> Signed-off-by: David Herrmann <dh.herrmann at googlemail.com>
> ---
>  src/wayland-private.h | 2 +-
>  src/wayland-util.c    | 4 +++-
>  2 files changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/src/wayland-private.h b/src/wayland-private.h
> index 8adee9f..406dd39 100644
> --- a/src/wayland-private.h
> +++ b/src/wayland-private.h
> @@ -44,7 +44,7 @@ typedef void (*wl_iterator_func_t)(void *element, void *data);
>  
>  void wl_map_init(struct wl_map *map);
>  void wl_map_release(struct wl_map *map);
> -uint32_t wl_map_insert_new(struct wl_map *map, uint32_t side, void *data);
> +int32_t wl_map_insert_new(struct wl_map *map, uint32_t side, void *data);
>  int wl_map_insert_at(struct wl_map *map, uint32_t i, void *data);
>  int wl_map_reserve_new(struct wl_map *map, uint32_t i);
>  void wl_map_remove(struct wl_map *map, uint32_t i);
> diff --git a/src/wayland-util.c b/src/wayland-util.c
> index 158f5a5..0ea6f58 100644
> --- a/src/wayland-util.c
> +++ b/src/wayland-util.c
> @@ -164,7 +164,7 @@ wl_map_release(struct wl_map *map)
>  	wl_array_release(&map->server_entries);
>  }
>  
> -WL_EXPORT uint32_t
> +WL_EXPORT int32_t
>  wl_map_insert_new(struct wl_map *map, uint32_t side, void *data)
>  {
>  	union map_entry *start, *entry;
> @@ -185,6 +185,8 @@ wl_map_insert_new(struct wl_map *map, uint32_t side, void *data)
>  		map->free_list = entry->next;
>  	} else {
>  		entry = wl_array_add(entries, sizeof *entry);
> +		if (!entry)
> +			return -1;
>  		start = entries->data;
>  	}
>  
> -- 
> 1.7.12.2
> 
> _______________________________________________
> wayland-devel mailing list
> wayland-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/wayland-devel


More information about the wayland-devel mailing list