[RFC wayland 02/12] wayland-util: return -1 if wl_array_copy() fails

David Herrmann dh.herrmann at googlemail.com
Thu Oct 11 14:37:43 PDT 2012


We might have to perform memory allocations in wl_array_copy(), so catch
out-of-memory errors in wl_array_add() and return -1 before changing any
state.

Signed-off-by: David Herrmann <dh.herrmann at googlemail.com>
---
 src/wayland-util.c | 12 +++++++++---
 src/wayland-util.h |  2 +-
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/wayland-util.c b/src/wayland-util.c
index eacf902..158f5a5 100644
--- a/src/wayland-util.c
+++ b/src/wayland-util.c
@@ -132,12 +132,18 @@ wl_array_add(struct wl_array *array, size_t size)
 	return p;
 }
 
-WL_EXPORT void
+WL_EXPORT int
 wl_array_copy(struct wl_array *array, struct wl_array *source)
 {
-	array->size = 0;
-	wl_array_add(array, source->size);
+	if (array->size < source->size) {
+		if (!wl_array_add(array, source->size - array->size))
+			return -1;
+	} else {
+		array->size = source->size;
+	}
+
 	memcpy(array->data, source->data, source->size);
+	return 0;
 }
 
 union map_entry {
diff --git a/src/wayland-util.h b/src/wayland-util.h
index b588505..f54077e 100644
--- a/src/wayland-util.h
+++ b/src/wayland-util.h
@@ -165,7 +165,7 @@ struct wl_array {
 void wl_array_init(struct wl_array *array);
 void wl_array_release(struct wl_array *array);
 void *wl_array_add(struct wl_array *array, size_t size);
-void wl_array_copy(struct wl_array *array, struct wl_array *source);
+int wl_array_copy(struct wl_array *array, struct wl_array *source);
 
 typedef int32_t wl_fixed_t;
 
-- 
1.7.12.2



More information about the wayland-devel mailing list