commit eb313c4

Michael Forney  ·  2020-01-03 04:00:16 +0000 UTC
parent 5db2b5a
data_device_manager: Add some missing error checks
4 files changed,  +50, -40
+28, -26
 1@@ -1,6 +1,6 @@
 2 /* swc: data.c
 3  *
 4- * Copyright (c) 2013, 2014 Michael Forney
 5+ * Copyright (c) 2013-2020 Michael Forney
 6  *
 7  * Permission is hereby granted, free of charge, to any person obtaining a copy
 8  * of this software and associated documentation files (the "Software"), to deal
 9@@ -70,10 +70,21 @@ static void
10 source_offer(struct wl_client *client, struct wl_resource *source, const char *mime_type)
11 {
12 	struct data *data = wl_resource_get_user_data(source);
13-	char **destination;
14-
15-	destination = wl_array_add(&data->mime_types, sizeof(*destination));
16-	*destination = strdup(mime_type);
17+	char *s, **dst;
18+
19+	s = strdup(mime_type);
20+	if (!s)
21+		goto error0;
22+	dst = wl_array_add(&data->mime_types, sizeof(*dst));
23+	if (!dst)
24+		goto error1;
25+	*dst = s;
26+	return;
27+
28+error1:
29+	free(s);
30+error0:
31+	wl_resource_post_no_memory(source);
32 }
33 
34 static const struct wl_data_source_interface data_source_impl = {
35@@ -108,39 +119,28 @@ data_destroy(struct wl_resource *source)
36 	free(data);
37 }
38 
39-static struct data *
40-data_new(void)
41+struct wl_resource *
42+data_source_new(struct wl_client *client, uint32_t version, uint32_t id)
43 {
44 	struct data *data;
45 
46 	data = malloc(sizeof(*data));
47-
48 	if (!data)
49-		return NULL;
50-
51+		goto error0;
52 	wl_array_init(&data->mime_types);
53 	wl_list_init(&data->offers);
54 
55-	return data;
56-}
57-
58-struct wl_resource *
59-data_source_new(struct wl_client *client, uint32_t version, uint32_t id)
60-{
61-	struct data *data;
62-
63-	data = data_new();
64-
65-	if (!data)
66-		return NULL;
67-
68-	/* Add the data source to the client. */
69 	data->source = wl_resource_create(client, &wl_data_source_interface, version, id);
70-
71-	/* Destroy the data object when the source disappears. */
72+	if (!data->source)
73+		goto error1;
74 	wl_resource_set_implementation(data->source, &data_source_impl, data, &data_destroy);
75 
76 	return data->source;
77+
78+error1:
79+	free(data);
80+error0:
81+	return NULL;
82 }
83 
84 struct wl_resource *
85@@ -150,6 +150,8 @@ data_offer_new(struct wl_client *client, struct wl_resource *source, uint32_t ve
86 	struct wl_resource *offer;
87 
88 	offer = wl_resource_create(client, &wl_data_offer_interface, version, 0);
89+	if (!offer)
90+		return NULL;
91 	wl_resource_set_implementation(offer, &data_offer_impl, data, &remove_resource);
92 	wl_list_insert(&data->offers, wl_resource_get_link(offer));
93 
+12, -5
 1@@ -1,6 +1,6 @@
 2 /* swc: data_device.c
 3  *
 4- * Copyright (c) 2013 Michael Forney
 5+ * Copyright (c) 2013-2020 Michael Forney
 6  *
 7  * Permission is hereby granted, free of charge, to any person obtaining a copy
 8  * of this software and associated documentation files (the "Software"), to deal
 9@@ -97,14 +97,18 @@ data_device_destroy(struct data_device *data_device)
10 	free(data_device);
11 }
12 
13-void
14+struct wl_resource *
15 data_device_bind(struct data_device *data_device, struct wl_client *client, uint32_t version, uint32_t id)
16 {
17 	struct wl_resource *resource;
18 
19 	resource = wl_resource_create(client, &wl_data_device_interface, version, id);
20+	if (!resource)
21+		return NULL;
22 	wl_resource_set_implementation(resource, &data_device_impl, data_device, &remove_resource);
23 	wl_list_insert(&data_device->resources, &resource->link);
24+
25+	return resource;
26 }
27 
28 static struct wl_resource *
29@@ -113,6 +117,8 @@ new_offer(struct wl_resource *resource, struct wl_client *client, struct wl_reso
30 	struct wl_resource *offer;
31 
32 	offer = data_offer_new(client, source, wl_resource_get_version(resource));
33+	if (!offer)
34+		return NULL;
35 	wl_data_device_send_data_offer(resource, offer);
36 	data_send_mime_types(source, offer);
37 
38@@ -123,7 +129,7 @@ void
39 data_device_offer_selection(struct data_device *data_device, struct wl_client *client)
40 {
41 	struct wl_resource *resource;
42-	struct wl_resource *offer;
43+	struct wl_resource *offer = NULL;
44 
45 	/* Look for the client's data_device resource. */
46 	resource = wl_resource_find_for_client(&data_device->resources, client);
47@@ -132,8 +138,9 @@ data_device_offer_selection(struct data_device *data_device, struct wl_client *c
48 	if (!resource)
49 		return;
50 
51-	/* If we don't have a selection, send NULL to the client. */
52-	offer = data_device->selection ? new_offer(resource, client, data_device->selection) : NULL;
53+	/* If we have a selection, create a new offer for the client. */
54+	if (data_device->selection)
55+		offer = new_offer(resource, client, data_device->selection);
56 
57 	wl_data_device_send_selection(resource, offer);
58 }
+2, -2
 1@@ -1,6 +1,6 @@
 2 /* swc: data_device.h
 3  *
 4- * Copyright (c) 2013-2019 Michael Forney
 5+ * Copyright (c) 2013-2020 Michael Forney
 6  *
 7  * Permission is hereby granted, free of charge, to any person obtaining a copy
 8  * of this software and associated documentation files (the "Software"), to deal
 9@@ -42,7 +42,7 @@ struct data_device {
10 
11 struct data_device *data_device_create(void);
12 void data_device_destroy(struct data_device *data_device);
13-void data_device_bind(struct data_device *data_device, struct wl_client *client, uint32_t version, uint32_t id);
14+struct wl_resource *data_device_bind(struct data_device *data_device, struct wl_client *client, uint32_t version, uint32_t id);
15 void data_device_offer_selection(struct data_device *data_device, struct wl_client *client);
16 
17 #endif
+8, -7
 1@@ -1,6 +1,6 @@
 2 /* swc: data_device_manager.c
 3  *
 4- * Copyright (c) 2013-2019 Michael Forney
 5+ * Copyright (c) 2013-2020 Michael Forney
 6  *
 7  * Permission is hereby granted, free of charge, to any person obtaining a copy
 8  * of this software and associated documentation files (the "Software"), to deal
 9@@ -30,11 +30,7 @@
10 static void
11 create_data_source(struct wl_client *client, struct wl_resource *resource, uint32_t id)
12 {
13-	struct wl_resource *data_source;
14-
15-	data_source = data_source_new(client, wl_resource_get_version(resource), id);
16-
17-	if (!data_source)
18+	if (!data_source_new(client, wl_resource_get_version(resource), id))
19 		wl_resource_post_no_memory(resource);
20 }
21 
22@@ -43,7 +39,8 @@ get_data_device(struct wl_client *client, struct wl_resource *resource, uint32_t
23 {
24 	struct swc_seat *seat = wl_resource_get_user_data(seat_resource);
25 
26-	data_device_bind(seat->data_device, client, wl_resource_get_version(resource), id);
27+	if (!data_device_bind(seat->data_device, client, wl_resource_get_version(resource), id))
28+		wl_resource_post_no_memory(resource);
29 }
30 
31 static const struct wl_data_device_manager_interface data_device_manager_impl = {
32@@ -57,6 +54,10 @@ bind_data_device_manager(struct wl_client *client, void *data, uint32_t version,
33 	struct wl_resource *resource;
34 
35 	resource = wl_resource_create(client, &wl_data_device_manager_interface, version, id);
36+	if (!resource) {
37+		wl_client_post_no_memory(client);
38+		return;
39+	}
40 	wl_resource_set_implementation(resource, &data_device_manager_impl, NULL, NULL);
41 }
42