commit eb313c45b95d766cee1515d5fe5c07f17ed0132f
parent 5db2b5a16cef8259a40dac59087b3f41cd390b57
Author: Michael Forney <mforney@mforney.org>
Date: Thu, 2 Jan 2020 20:00:16 -0800
data_device_manager: Add some missing error checks
Diffstat:
4 files changed, 50 insertions(+), 40 deletions(-)
diff --git a/libswc/data.c b/libswc/data.c
@@ -1,6 +1,6 @@
/* swc: data.c
*
- * Copyright (c) 2013, 2014 Michael Forney
+ * Copyright (c) 2013-2020 Michael Forney
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -70,10 +70,21 @@ static void
source_offer(struct wl_client *client, struct wl_resource *source, const char *mime_type)
{
struct data *data = wl_resource_get_user_data(source);
- char **destination;
-
- destination = wl_array_add(&data->mime_types, sizeof(*destination));
- *destination = strdup(mime_type);
+ char *s, **dst;
+
+ s = strdup(mime_type);
+ if (!s)
+ goto error0;
+ dst = wl_array_add(&data->mime_types, sizeof(*dst));
+ if (!dst)
+ goto error1;
+ *dst = s;
+ return;
+
+error1:
+ free(s);
+error0:
+ wl_resource_post_no_memory(source);
}
static const struct wl_data_source_interface data_source_impl = {
@@ -108,39 +119,28 @@ data_destroy(struct wl_resource *source)
free(data);
}
-static struct data *
-data_new(void)
+struct wl_resource *
+data_source_new(struct wl_client *client, uint32_t version, uint32_t id)
{
struct data *data;
data = malloc(sizeof(*data));
-
if (!data)
- return NULL;
-
+ goto error0;
wl_array_init(&data->mime_types);
wl_list_init(&data->offers);
- return data;
-}
-
-struct wl_resource *
-data_source_new(struct wl_client *client, uint32_t version, uint32_t id)
-{
- struct data *data;
-
- data = data_new();
-
- if (!data)
- return NULL;
-
- /* Add the data source to the client. */
data->source = wl_resource_create(client, &wl_data_source_interface, version, id);
-
- /* Destroy the data object when the source disappears. */
+ if (!data->source)
+ goto error1;
wl_resource_set_implementation(data->source, &data_source_impl, data, &data_destroy);
return data->source;
+
+error1:
+ free(data);
+error0:
+ return NULL;
}
struct wl_resource *
@@ -150,6 +150,8 @@ data_offer_new(struct wl_client *client, struct wl_resource *source, uint32_t ve
struct wl_resource *offer;
offer = wl_resource_create(client, &wl_data_offer_interface, version, 0);
+ if (!offer)
+ return NULL;
wl_resource_set_implementation(offer, &data_offer_impl, data, &remove_resource);
wl_list_insert(&data->offers, wl_resource_get_link(offer));
diff --git a/libswc/data_device.c b/libswc/data_device.c
@@ -1,6 +1,6 @@
/* swc: data_device.c
*
- * Copyright (c) 2013 Michael Forney
+ * Copyright (c) 2013-2020 Michael Forney
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -97,14 +97,18 @@ data_device_destroy(struct data_device *data_device)
free(data_device);
}
-void
+struct wl_resource *
data_device_bind(struct data_device *data_device, struct wl_client *client, uint32_t version, uint32_t id)
{
struct wl_resource *resource;
resource = wl_resource_create(client, &wl_data_device_interface, version, id);
+ if (!resource)
+ return NULL;
wl_resource_set_implementation(resource, &data_device_impl, data_device, &remove_resource);
wl_list_insert(&data_device->resources, &resource->link);
+
+ return resource;
}
static struct wl_resource *
@@ -113,6 +117,8 @@ new_offer(struct wl_resource *resource, struct wl_client *client, struct wl_reso
struct wl_resource *offer;
offer = data_offer_new(client, source, wl_resource_get_version(resource));
+ if (!offer)
+ return NULL;
wl_data_device_send_data_offer(resource, offer);
data_send_mime_types(source, offer);
@@ -123,7 +129,7 @@ void
data_device_offer_selection(struct data_device *data_device, struct wl_client *client)
{
struct wl_resource *resource;
- struct wl_resource *offer;
+ struct wl_resource *offer = NULL;
/* Look for the client's data_device resource. */
resource = wl_resource_find_for_client(&data_device->resources, client);
@@ -132,8 +138,9 @@ data_device_offer_selection(struct data_device *data_device, struct wl_client *c
if (!resource)
return;
- /* If we don't have a selection, send NULL to the client. */
- offer = data_device->selection ? new_offer(resource, client, data_device->selection) : NULL;
+ /* If we have a selection, create a new offer for the client. */
+ if (data_device->selection)
+ offer = new_offer(resource, client, data_device->selection);
wl_data_device_send_selection(resource, offer);
}
diff --git a/libswc/data_device.h b/libswc/data_device.h
@@ -1,6 +1,6 @@
/* swc: data_device.h
*
- * Copyright (c) 2013-2019 Michael Forney
+ * Copyright (c) 2013-2020 Michael Forney
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -42,7 +42,7 @@ struct data_device {
struct data_device *data_device_create(void);
void data_device_destroy(struct data_device *data_device);
-void data_device_bind(struct data_device *data_device, struct wl_client *client, uint32_t version, uint32_t id);
+struct wl_resource *data_device_bind(struct data_device *data_device, struct wl_client *client, uint32_t version, uint32_t id);
void data_device_offer_selection(struct data_device *data_device, struct wl_client *client);
#endif
diff --git a/libswc/data_device_manager.c b/libswc/data_device_manager.c
@@ -1,6 +1,6 @@
/* swc: data_device_manager.c
*
- * Copyright (c) 2013-2019 Michael Forney
+ * Copyright (c) 2013-2020 Michael Forney
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -30,11 +30,7 @@
static void
create_data_source(struct wl_client *client, struct wl_resource *resource, uint32_t id)
{
- struct wl_resource *data_source;
-
- data_source = data_source_new(client, wl_resource_get_version(resource), id);
-
- if (!data_source)
+ if (!data_source_new(client, wl_resource_get_version(resource), id))
wl_resource_post_no_memory(resource);
}
@@ -43,7 +39,8 @@ get_data_device(struct wl_client *client, struct wl_resource *resource, uint32_t
{
struct swc_seat *seat = wl_resource_get_user_data(seat_resource);
- data_device_bind(seat->data_device, client, wl_resource_get_version(resource), id);
+ if (!data_device_bind(seat->data_device, client, wl_resource_get_version(resource), id))
+ wl_resource_post_no_memory(resource);
}
static const struct wl_data_device_manager_interface data_device_manager_impl = {
@@ -57,6 +54,10 @@ bind_data_device_manager(struct wl_client *client, void *data, uint32_t version,
struct wl_resource *resource;
resource = wl_resource_create(client, &wl_data_device_manager_interface, version, id);
+ if (!resource) {
+ wl_client_post_no_memory(client);
+ return;
+ }
wl_resource_set_implementation(resource, &data_device_manager_impl, NULL, NULL);
}