From c348790ca1bc24398899ef14fd70db08334fe3bf Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Wed, 15 May 2019 15:19:25 +0200 Subject: [PATCH] map: handle more error cases --- spa/plugins/bluez5/defs.h | 12 +++++++----- src/pipewire/array.h | 10 ++++++---- src/pipewire/map.h | 24 +++++++++++++++--------- src/pipewire/resource.c | 2 +- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/spa/plugins/bluez5/defs.h b/spa/plugins/bluez5/defs.h index e07bdab76..160944956 100644 --- a/spa/plugins/bluez5/defs.h +++ b/spa/plugins/bluez5/defs.h @@ -230,11 +230,13 @@ struct spa_bt_transport { #define spa_bt_transport_set_implementation(t,_impl,_data) \ (t)->impl = SPA_CALLBACKS_INIT(_impl, _data) -#define spa_bt_transport_impl(t,m,v,...) \ -({ \ - int res = 0; \ - spa_callbacks_call_res(&(t)->impl, struct spa_bt_transport_implementation, res, m, v, ##__VA_ARGS__); \ - res; \ +#define spa_bt_transport_impl(t,m,v,...) \ +({ \ + int res = 0; \ + spa_callbacks_call_res(&(t)->impl, \ + struct spa_bt_transport_implementation, \ + res, m, v, ##__VA_ARGS__); \ + res; \ }) #define spa_bt_transport_acquire(t,o) spa_bt_transport_impl(t, acquire, 0, o) diff --git a/src/pipewire/array.h b/src/pipewire/array.h index 3ffc07ca8..f8ba5699d 100644 --- a/src/pipewire/array.h +++ b/src/pipewire/array.h @@ -29,6 +29,8 @@ extern "C" { #endif +#include + #include /** \class pw_array @@ -94,7 +96,7 @@ static inline void pw_array_reset(struct pw_array *arr) } /** Make sure \a size bytes can be added to the array \memberof pw_array */ -static inline bool pw_array_ensure_size(struct pw_array *arr, size_t size) +static inline int pw_array_ensure_size(struct pw_array *arr, size_t size) { size_t alloc, need; @@ -107,11 +109,11 @@ static inline bool pw_array_ensure_size(struct pw_array *arr, size_t size) while (alloc < need) alloc *= 2; if (SPA_UNLIKELY((data = realloc(arr->data, alloc)) == NULL)) - return false; + return -errno; arr->data = data; arr->alloc = alloc; } - return true; + return 0; } /** Add \a ref size bytes to \a arr. A pointer to memory that can @@ -120,7 +122,7 @@ static inline void *pw_array_add(struct pw_array *arr, size_t size) { void *p; - if (!pw_array_ensure_size(arr, size)) + if (pw_array_ensure_size(arr, size) < 0) return NULL; p = SPA_MEMBER(arr->data, arr->size, void); diff --git a/src/pipewire/map.h b/src/pipewire/map.h index 1bee596ae..ba4c01bc0 100644 --- a/src/pipewire/map.h +++ b/src/pipewire/map.h @@ -30,6 +30,7 @@ extern "C" { #endif #include +#include #include #include @@ -97,7 +98,8 @@ static inline void pw_map_reset(struct pw_map *map) /** Insert data in the map * \param map the map to insert into * \param data the item to add - * \return the id where the item was inserted + * \return the id where the item was inserted or SPA_ID_INVALID when the + * item can not be inserted. * \memberof pw_map */ static inline uint32_t pw_map_insert_new(struct pw_map *map, void *data) @@ -111,7 +113,7 @@ static inline uint32_t pw_map_insert_new(struct pw_map *map, void *data) map->free_list = item->next; } else { item = (union pw_map_item *) pw_array_add(&map->items, sizeof(union pw_map_item)); - if (!item) + if (item == NULL) return SPA_ID_INVALID; start = (union pw_map_item *) map->items.data; } @@ -124,23 +126,27 @@ static inline uint32_t pw_map_insert_new(struct pw_map *map, void *data) * \param map the map to inser into * \param id the index to insert at * \param data the data to insert - * \return true on success, false when the index is invalid + * \return 0 on success, -ENOSPC value when the index is invalid or a < 0 + * errno value. * \memberof pw_map */ -static inline bool pw_map_insert_at(struct pw_map *map, uint32_t id, void *data) +static inline int pw_map_insert_at(struct pw_map *map, uint32_t id, void *data) { size_t size = pw_map_get_size(map); union pw_map_item *item; if (id > size) - return false; - else if (id == size) + return -ENOSPC; + else if (id == size) { item = (union pw_map_item *) pw_array_add(&map->items, sizeof(union pw_map_item)); - else + if (item == NULL) + return -errno; + } + else { item = pw_map_get_item(map, id); - + } item->data = data; - return true; + return 0; } /** Remove an item at index diff --git a/src/pipewire/resource.c b/src/pipewire/resource.c index ec640ca44..46f170926 100644 --- a/src/pipewire/resource.c +++ b/src/pipewire/resource.c @@ -66,7 +66,7 @@ struct pw_resource *pw_resource_new(struct pw_client *client, if (id == SPA_ID_INVALID) { id = pw_map_insert_new(&client->objects, this); - } else if (!pw_map_insert_at(&client->objects, id, this)) + } else if (pw_map_insert_at(&client->objects, id, this) < 0) goto in_use; this->id = id;