[PATCH libinput 3/3] Add safe_strdup()
Peter Hutterer
peter.hutterer at who-t.net
Fri Jul 7 01:55:55 UTC 2017
Return value is either NULL or a strdup'd string, depending on the input
value.
Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
meson.build | 2 ++
src/libinput-util.h | 19 +++++++++++++++++++
src/libinput.c | 12 +++---------
src/path-seat.c | 7 +++----
src/udev-seat.c | 3 +--
test/litest.c | 13 ++++++-------
test/test-path.c | 5 +++--
tools/shared.c | 2 +-
8 files changed, 38 insertions(+), 25 deletions(-)
diff --git a/meson.build b/meson.build
index 9b0fa246..abc8d765 100644
--- a/meson.build
+++ b/meson.build
@@ -625,6 +625,8 @@ if get_option('tests')
args : [ meson.current_source_dir() ])
libinput_test_runner_sources = [
+ 'src/libinput-util.h',
+ 'src/libinput-util.c',
'test/test-udev.c',
'test/test-path.c',
'test/test-pointer.c',
diff --git a/src/libinput-util.h b/src/libinput-util.h
index 264eea73..b984f9f1 100644
--- a/src/libinput-util.h
+++ b/src/libinput-util.h
@@ -145,6 +145,25 @@ zalloc(size_t size)
return p;
}
+/**
+ * strdup guaranteed to succeed. If the input string is NULL, the output
+ * string is NULL. If the input string is a string pointer, we strdup or
+ * abort on failure.
+ */
+static inline char*
+safe_strdup(const char *str)
+{
+ char *s;
+
+ if (!str)
+ return NULL;
+
+ s = strdup(str);
+ if (!s)
+ abort();
+ return s;
+}
+
/* This bitfield helper implementation is taken from from libevdev-util.h,
* except that it has been modified to work with arrays of unsigned chars
*/
diff --git a/src/libinput.c b/src/libinput.c
index 4187c166..1ca737a0 100644
--- a/src/libinput.c
+++ b/src/libinput.c
@@ -1861,8 +1861,8 @@ libinput_seat_init(struct libinput_seat *seat,
{
seat->refcount = 1;
seat->libinput = libinput;
- seat->physical_name = strdup(physical_name);
- seat->logical_name = strdup(logical_name);
+ seat->physical_name = safe_strdup(physical_name);
+ seat->logical_name = safe_strdup(logical_name);
seat->destroy = destroy;
list_init(&seat->devices_list);
list_insert(&libinput->seat_list, &seat->link);
@@ -3319,13 +3319,7 @@ libinput_device_group_create(struct libinput *libinput,
group = zalloc(sizeof *group);
group->refcount = 1;
- if (identifier) {
- group->identifier = strdup(identifier);
- if (!group->identifier) {
- free(group);
- return NULL;
- }
- }
+ group->identifier = safe_strdup(identifier);
list_init(&group->link);
list_insert(&libinput->device_group_list, &group->link);
diff --git a/src/path-seat.c b/src/path-seat.c
index a0749732..c162dc64 100644
--- a/src/path-seat.c
+++ b/src/path-seat.c
@@ -121,10 +121,10 @@ path_device_enable(struct path_input *input,
sysname = udev_device_get_sysname(udev_device);
seat_prop = udev_device_get_property_value(udev_device, "ID_SEAT");
- seat_name = strdup(seat_prop ? seat_prop : default_seat);
+ seat_name = safe_strdup(seat_prop ? seat_prop : default_seat);
if (seat_logical_name_override) {
- seat_logical_name = strdup(seat_logical_name_override);
+ seat_logical_name = safe_strdup(seat_logical_name_override);
} else {
seat_prop = udev_device_get_property_value(udev_device, "WL_SEAT");
seat_logical_name = strdup(seat_prop ? seat_prop : default_seat_name);
@@ -173,8 +173,7 @@ path_device_enable(struct path_input *input,
evdev_read_calibration_prop(device);
output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
- if (output_name)
- device->output_name = strdup(output_name);
+ device->output_name = safe_strdup(output_name);
out:
free(seat_name);
diff --git a/src/udev-seat.c b/src/udev-seat.c
index b954e3ca..988ac3bf 100644
--- a/src/udev-seat.c
+++ b/src/udev-seat.c
@@ -102,8 +102,7 @@ device_added(struct udev_device *udev_device,
evdev_read_calibration_prop(device);
output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT");
- if (output_name)
- device->output_name = strdup(output_name);
+ device->output_name = strdup(output_name);
return 0;
}
diff --git a/test/litest.c b/test/litest.c
index 23395413..902d9ce5 100644
--- a/test/litest.c
+++ b/test/litest.c
@@ -524,8 +524,8 @@ litest_add_tcase_for_device(struct suite *suite,
struct test *t;
t = zalloc(sizeof(*t));
- t->name = strdup(funcname);
- t->devname = strdup(dev->shortname);
+ t->name = safe_strdup(funcname);
+ t->devname = safe_strdup(dev->shortname);
t->func = func;
t->setup = dev->setup;
t->teardown = dev->teardown ?
@@ -549,8 +549,8 @@ litest_add_tcase_no_device(struct suite *suite,
return;
t = zalloc(sizeof(*t));
- t->name = strdup(test_name);
- t->devname = strdup("no device");
+ t->name = safe_strdup(test_name);
+ t->devname = safe_strdup("no device");
t->func = func;
if (range)
t->range = *range;
@@ -571,7 +571,7 @@ get_suite(const char *name)
}
s = zalloc(sizeof(*s));
- s->name = strdup(name);
+ s->name = safe_strdup(name);
list_init(&s->tests);
list_insert(&all_tests, &s->node);
@@ -1133,8 +1133,7 @@ litest_copy_file(const char *dest, const char *src, const char *header)
int suffixlen;
file = zalloc(sizeof(*file));
- file->path = strdup(dest);
- litest_assert(file->path);
+ file->path = safe_strdup(dest);
suffixlen = file->path + strlen(file->path) - rindex(file->path, '.');
out = mkstemps(file->path, suffixlen);
diff --git a/test/test-path.c b/test/test-path.c
index 66f41ae0..d4087f1a 100644
--- a/test/test-path.c
+++ b/test/test-path.c
@@ -32,6 +32,7 @@
#include <unistd.h>
#include "litest.h"
+#include "libinput-util.h"
struct counter {
int open_func_count;
@@ -393,7 +394,7 @@ START_TEST(path_add_device)
ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
device = libinput_event_get_device(event);
ck_assert_notnull(device);
- sysname1 = strdup(libinput_device_get_sysname(device));
+ sysname1 = safe_strdup(libinput_device_get_sysname(device));
libinput_event_destroy(event);
litest_assert_empty_queue(li);
@@ -410,7 +411,7 @@ START_TEST(path_add_device)
ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
device = libinput_event_get_device(event);
ck_assert_notnull(device);
- sysname2 = strdup(libinput_device_get_sysname(device));
+ sysname2 = safe_strdup(libinput_device_get_sysname(device));
libinput_event_destroy(event);
ck_assert_str_eq(sysname1, sysname2);
diff --git a/tools/shared.c b/tools/shared.c
index db6e70ce..998782ff 100644
--- a/tools/shared.c
+++ b/tools/shared.c
@@ -397,7 +397,7 @@ find_device(const char *udev_tag)
}
if (udev_device_get_property_value(device, udev_tag))
- device_node = strdup(udev_device_get_devnode(device));
+ device_node = safe_strdup(udev_device_get_devnode(device));
udev_device_unref(device);
--
2.13.0
More information about the wayland-devel
mailing list