Add safe_strdup()

Return value is either NULL or a strdup'd string, depending on the input
value.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2017-07-07 09:47:06 +10:00
parent e71ade2755
commit 2e4895b888
9 changed files with 40 additions and 27 deletions

View file

@ -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',

View file

@ -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
*/

View file

@ -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);

View file

@ -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);

View file

@ -42,7 +42,7 @@ libinput_timer_init(struct libinput_timer *timer,
{
timer->libinput = libinput;
if (timer_name)
timer->timer_name = strdup(timer_name);
timer->timer_name = safe_strdup(timer_name);
timer->timer_func = timer_func;
timer->timer_func_data = timer_func_data;
}

View file

@ -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 = safe_strdup(output_name);
return 0;
}
@ -383,7 +382,7 @@ libinput_udev_assign_seat(struct libinput *libinput,
if (input->seat_id != NULL)
return -1;
input->seat_id = strdup(seat_id);
input->seat_id = safe_strdup(seat_id);
if (udev_input_enable(&input->base) < 0)
return -1;

View file

@ -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);

View file

@ -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);

View file

@ -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);