[PATCH libinput 1/3] Abort if zalloc ever fails

Peter Hutterer peter.hutterer at who-t.net
Fri Jul 7 01:55:53 UTC 2017


There's no guarantee that libinput does the right thing if memory allocation
fails and it's such a niche case on the systems we're targeting that it just
doesn't matter. Simply abort if zalloc ever fails.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 src/evdev-lid.c                       |  6 ++---
 src/evdev-mt-touchpad.c               |  2 --
 src/evdev-tablet-pad-leds.c           |  9 -------
 src/evdev-tablet-pad.c                |  2 --
 src/evdev-tablet.c                    |  5 +---
 src/evdev.c                           |  8 ++----
 src/filter.c                          | 12 ---------
 src/libinput-util.c                   |  2 --
 src/libinput-util.h                   |  8 +++++-
 src/libinput.c                        | 51 -----------------------------------
 src/path-seat.c                       |  8 +-----
 src/udev-seat.c                       |  4 ---
 test/litest-device-atmel-hover.c      |  5 ++--
 test/litest.c                         |  8 ------
 tools/libinput-measure-touchpad-tap.c |  6 ++---
 15 files changed, 19 insertions(+), 117 deletions(-)

diff --git a/src/evdev-lid.c b/src/evdev-lid.c
index fe98e6e1..b3df37c2 100644
--- a/src/evdev-lid.c
+++ b/src/evdev-lid.c
@@ -297,11 +297,9 @@ struct evdev_dispatch_interface lid_switch_interface = {
 struct evdev_dispatch *
 evdev_lid_switch_dispatch_create(struct evdev_device *lid_device)
 {
-	struct lid_switch_dispatch *dispatch = zalloc(sizeof *dispatch);
-
-	if (dispatch == NULL)
-		return NULL;
+	struct lid_switch_dispatch *dispatch;
 
+	dispatch = zalloc(sizeof *dispatch);
 	dispatch->base.dispatch_type = DISPATCH_LID_SWITCH;
 	dispatch->base.interface = &lid_switch_interface;
 	dispatch->device = lid_device;
diff --git a/src/evdev-mt-touchpad.c b/src/evdev-mt-touchpad.c
index 4d0cff97..4a6e47cc 100644
--- a/src/evdev-mt-touchpad.c
+++ b/src/evdev-mt-touchpad.c
@@ -2727,8 +2727,6 @@ evdev_mt_touchpad_create(struct evdev_device *device)
 	evdev_tag_touchpad(device, device->udev_device);
 
 	tp = zalloc(sizeof *tp);
-	if (!tp)
-		return NULL;
 
 	if (!tp_init(tp, device)) {
 		tp_interface_destroy(&tp->base);
diff --git a/src/evdev-tablet-pad-leds.c b/src/evdev-tablet-pad-leds.c
index 82f8edb0..00863800 100644
--- a/src/evdev-tablet-pad-leds.c
+++ b/src/evdev-tablet-pad-leds.c
@@ -59,9 +59,6 @@ pad_mode_toggle_button_new(struct pad_dispatch *pad,
 	struct pad_mode_toggle_button *button;
 
 	button = zalloc(sizeof *button);
-	if (!button)
-		return NULL;
-
 	button->button_index = button_index;
 
 	return button;
@@ -121,9 +118,6 @@ pad_led_new(struct libinput *libinput, const char *prefix, int group, int mode)
 	int rc, fd;
 
 	led = zalloc(sizeof *led);
-	if (!led)
-		return NULL;
-
 	led->brightness_fd = -1;
 	led->mode_idx = mode;
 	list_init(&led->link);
@@ -178,9 +172,6 @@ pad_group_new_basic(struct pad_dispatch *pad,
 	struct pad_led_group *group;
 
 	group = zalloc(sizeof *group);
-	if (!group)
-		return NULL;
-
 	group->base.device = &pad->device->base;
 	group->base.refcount = 1;
 	group->base.index = group_index;
diff --git a/src/evdev-tablet-pad.c b/src/evdev-tablet-pad.c
index 2b8c1422..092defa6 100644
--- a/src/evdev-tablet-pad.c
+++ b/src/evdev-tablet-pad.c
@@ -628,8 +628,6 @@ evdev_tablet_pad_create(struct evdev_device *device)
 	struct pad_dispatch *pad;
 
 	pad = zalloc(sizeof *pad);
-	if (!pad)
-		return NULL;
 
 	if (pad_init(pad, device) != 0) {
 		pad_destroy(&pad->base);
diff --git a/src/evdev-tablet.c b/src/evdev-tablet.c
index 40bc999c..77540496 100644
--- a/src/evdev-tablet.c
+++ b/src/evdev-tablet.c
@@ -1035,8 +1035,7 @@ tablet_get_tool(struct tablet_dispatch *tablet,
 		const struct input_absinfo *pressure;
 
 		tool = zalloc(sizeof *tool);
-		if (!tool)
-			return NULL;
+
 		*tool = (struct libinput_tablet_tool) {
 			.type = type,
 			.serial = serial,
@@ -1932,8 +1931,6 @@ evdev_tablet_create(struct evdev_device *device)
 	struct tablet_dispatch *tablet;
 
 	tablet = zalloc(sizeof *tablet);
-	if (!tablet)
-		return NULL;
 
 	if (tablet_init(tablet, device) != 0) {
 		tablet_destroy(&tablet->base);
diff --git a/src/evdev.c b/src/evdev.c
index a6804149..e9326185 100644
--- a/src/evdev.c
+++ b/src/evdev.c
@@ -1830,12 +1830,10 @@ fallback_dispatch_init_abs(struct fallback_dispatch *dispatch,
 static struct evdev_dispatch *
 fallback_dispatch_create(struct libinput_device *libinput_device)
 {
-	struct fallback_dispatch *dispatch = zalloc(sizeof *dispatch);
 	struct evdev_device *device = evdev_device(libinput_device);
+	struct fallback_dispatch *dispatch;
 
-	if (dispatch == NULL)
-		return NULL;
-
+	dispatch = zalloc(sizeof *dispatch);
 	dispatch->base.dispatch_type = DISPATCH_FALLBACK;
 	dispatch->base.interface = &fallback_interface;
 	dispatch->pending_event = EVDEV_NONE;
@@ -2929,8 +2927,6 @@ evdev_device_create(struct libinput_seat *seat,
 		goto err;
 
 	device = zalloc(sizeof *device);
-	if (device == NULL)
-		goto err;
 
 	libinput_device_init(&device->base, seat);
 	libinput_seat_ref(seat);
diff --git a/src/filter.c b/src/filter.c
index 7c500f87..ba4da5f1 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -951,9 +951,6 @@ create_default_filter(int dpi)
 	struct pointer_accelerator *filter;
 
 	filter = zalloc(sizeof *filter);
-	if (filter == NULL)
-		return NULL;
-
 	filter->last_velocity = 0.0;
 
 	filter->trackers =
@@ -1050,9 +1047,6 @@ create_pointer_accelerator_filter_lenovo_x230(int dpi)
 	struct pointer_accelerator *filter;
 
 	filter = zalloc(sizeof *filter);
-	if (filter == NULL)
-		return NULL;
-
 	filter->base.interface = &accelerator_interface_x230;
 	filter->profile = touchpad_lenovo_x230_accel_profile;
 	filter->last_velocity = 0.0;
@@ -1160,9 +1154,6 @@ create_pointer_accelerator_filter_flat(int dpi)
 	struct pointer_accelerator_flat *filter;
 
 	filter = zalloc(sizeof *filter);
-	if (filter == NULL)
-		return NULL;
-
 	filter->base.interface = &accelerator_interface_flat;
 	filter->dpi = dpi;
 
@@ -1284,9 +1275,6 @@ create_tablet_filter_flat(int xres, int yres)
 	struct tablet_accelerator_flat *filter;
 
 	filter = zalloc(sizeof *filter);
-	if (filter == NULL)
-		return NULL;
-
 	filter->factor = 1.0;
 	filter->xres = xres;
 	filter->yres = yres;
diff --git a/src/libinput-util.c b/src/libinput-util.c
index 9104e9d9..ac7a5d54 100644
--- a/src/libinput-util.c
+++ b/src/libinput-util.c
@@ -494,8 +494,6 @@ strv_from_string(const char *in, const char *separators)
 
 	nelems++; /* NULL-terminated */
 	strv = zalloc(nelems * sizeof *strv);
-	if (!strv)
-		return NULL;
 
 	idx = 0;
 
diff --git a/src/libinput-util.h b/src/libinput-util.h
index 123dbf03..264eea73 100644
--- a/src/libinput-util.h
+++ b/src/libinput-util.h
@@ -136,7 +136,13 @@ bool list_empty(const struct list *list);
 static inline void *
 zalloc(size_t size)
 {
-	return calloc(1, size);
+	void *p;
+
+	p = calloc(1, size);
+	if (!p)
+		abort();
+
+	return p;
 }
 
 /* This bitfield helper implementation is taken from from libevdev-util.h,
diff --git a/src/libinput.c b/src/libinput.c
index f221b6a0..4187c166 100644
--- a/src/libinput.c
+++ b/src/libinput.c
@@ -1648,9 +1648,6 @@ libinput_add_fd(struct libinput *libinput,
 	struct epoll_event ep;
 
 	source = zalloc(sizeof *source);
-	if (!source)
-		return NULL;
-
 	source->dispatch = dispatch;
 	source->user_data = user_data;
 	source->fd = fd;
@@ -1691,11 +1688,6 @@ libinput_init(struct libinput *libinput,
 
 	libinput->events_len = 4;
 	libinput->events = zalloc(libinput->events_len * sizeof(*libinput->events));
-	if (!libinput->events) {
-		close(libinput->epoll_fd);
-		return -1;
-	}
-
 	libinput->log_handler = libinput_default_log_func;
 	libinput->log_priority = LIBINPUT_LOG_PRIORITY_ERROR;
 	libinput->interface = interface;
@@ -2121,8 +2113,6 @@ notify_added_device(struct libinput_device *device)
 	struct libinput_event_device_notify *added_device_event;
 
 	added_device_event = zalloc(sizeof *added_device_event);
-	if (!added_device_event)
-		return;
 
 	post_base_event(device,
 			LIBINPUT_EVENT_DEVICE_ADDED,
@@ -2135,8 +2125,6 @@ notify_removed_device(struct libinput_device *device)
 	struct libinput_event_device_notify *removed_device_event;
 
 	removed_device_event = zalloc(sizeof *removed_device_event);
-	if (!removed_device_event)
-		return;
 
 	post_base_event(device,
 			LIBINPUT_EVENT_DEVICE_REMOVED,
@@ -2197,8 +2185,6 @@ keyboard_notify_key(struct libinput_device *device,
 		return;
 
 	key_event = zalloc(sizeof *key_event);
-	if (!key_event)
-		return;
 
 	seat_key_count = update_seat_key_count(device->seat, key, state);
 
@@ -2226,8 +2212,6 @@ pointer_notify_motion(struct libinput_device *device,
 		return;
 
 	motion_event = zalloc(sizeof *motion_event);
-	if (!motion_event)
-		return;
 
 	*motion_event = (struct libinput_event_pointer) {
 		.time = time,
@@ -2251,8 +2235,6 @@ pointer_notify_motion_absolute(struct libinput_device *device,
 		return;
 
 	motion_absolute_event = zalloc(sizeof *motion_absolute_event);
-	if (!motion_absolute_event)
-		return;
 
 	*motion_absolute_event = (struct libinput_event_pointer) {
 		.time = time,
@@ -2277,8 +2259,6 @@ pointer_notify_button(struct libinput_device *device,
 		return;
 
 	button_event = zalloc(sizeof *button_event);
-	if (!button_event)
-		return;
 
 	seat_button_count = update_seat_button_count(device->seat,
 						     button,
@@ -2310,8 +2290,6 @@ pointer_notify_axis(struct libinput_device *device,
 		return;
 
 	axis_event = zalloc(sizeof *axis_event);
-	if (!axis_event)
-		return;
 
 	*axis_event = (struct libinput_event_pointer) {
 		.time = time,
@@ -2339,8 +2317,6 @@ touch_notify_touch_down(struct libinput_device *device,
 		return;
 
 	touch_event = zalloc(sizeof *touch_event);
-	if (!touch_event)
-		return;
 
 	*touch_event = (struct libinput_event_touch) {
 		.time = time,
@@ -2367,8 +2343,6 @@ touch_notify_touch_motion(struct libinput_device *device,
 		return;
 
 	touch_event = zalloc(sizeof *touch_event);
-	if (!touch_event)
-		return;
 
 	*touch_event = (struct libinput_event_touch) {
 		.time = time,
@@ -2394,8 +2368,6 @@ touch_notify_touch_up(struct libinput_device *device,
 		return;
 
 	touch_event = zalloc(sizeof *touch_event);
-	if (!touch_event)
-		return;
 
 	*touch_event = (struct libinput_event_touch) {
 		.time = time,
@@ -2418,8 +2390,6 @@ touch_notify_frame(struct libinput_device *device,
 		return;
 
 	touch_event = zalloc(sizeof *touch_event);
-	if (!touch_event)
-		return;
 
 	*touch_event = (struct libinput_event_touch) {
 		.time = time,
@@ -2441,8 +2411,6 @@ tablet_notify_axis(struct libinput_device *device,
 	struct libinput_event_tablet_tool *axis_event;
 
 	axis_event = zalloc(sizeof *axis_event);
-	if (!axis_event)
-		return;
 
 	*axis_event = (struct libinput_event_tablet_tool) {
 		.time = time,
@@ -2473,8 +2441,6 @@ tablet_notify_proximity(struct libinput_device *device,
 	struct libinput_event_tablet_tool *proximity_event;
 
 	proximity_event = zalloc(sizeof *proximity_event);
-	if (!proximity_event)
-		return;
 
 	*proximity_event = (struct libinput_event_tablet_tool) {
 		.time = time,
@@ -2504,8 +2470,6 @@ tablet_notify_tip(struct libinput_device *device,
 	struct libinput_event_tablet_tool *tip_event;
 
 	tip_event = zalloc(sizeof *tip_event);
-	if (!tip_event)
-		return;
 
 	*tip_event = (struct libinput_event_tablet_tool) {
 		.time = time,
@@ -2537,8 +2501,6 @@ tablet_notify_button(struct libinput_device *device,
 	int32_t seat_button_count;
 
 	button_event = zalloc(sizeof *button_event);
-	if (!button_event)
-		return;
 
 	seat_button_count = update_seat_button_count(device->seat,
 						     button,
@@ -2572,8 +2534,6 @@ tablet_pad_notify_button(struct libinput_device *device,
 	unsigned int mode;
 
 	button_event = zalloc(sizeof *button_event);
-	if (!button_event)
-		return;
 
 	mode = libinput_tablet_pad_mode_group_get_mode(group);
 
@@ -2603,8 +2563,6 @@ tablet_pad_notify_ring(struct libinput_device *device,
 	unsigned int mode;
 
 	ring_event = zalloc(sizeof *ring_event);
-	if (!ring_event)
-		return;
 
 	mode = libinput_tablet_pad_mode_group_get_mode(group);
 
@@ -2635,8 +2593,6 @@ tablet_pad_notify_strip(struct libinput_device *device,
 	unsigned int mode;
 
 	strip_event = zalloc(sizeof *strip_event);
-	if (!strip_event)
-		return;
 
 	mode = libinput_tablet_pad_mode_group_get_mode(group);
 
@@ -2672,8 +2628,6 @@ gesture_notify(struct libinput_device *device,
 		return;
 
 	gesture_event = zalloc(sizeof *gesture_event);
-	if (!gesture_event)
-		return;
 
 	*gesture_event = (struct libinput_event_gesture) {
 		.time = time,
@@ -2752,8 +2706,6 @@ switch_notify_toggle(struct libinput_device *device,
 		return;
 
 	switch_event = zalloc(sizeof *switch_event);
-	if (!switch_event)
-		return;
 
 	*switch_event = (struct libinput_event_switch) {
 		.time = time,
@@ -3366,9 +3318,6 @@ libinput_device_group_create(struct libinput *libinput,
 	struct libinput_device_group *group;
 
 	group = zalloc(sizeof *group);
-	if (!group)
-		return NULL;
-
 	group->refcount = 1;
 	if (identifier) {
 		group->identifier = strdup(identifier);
diff --git a/src/path-seat.c b/src/path-seat.c
index f5e1af9f..a0749732 100644
--- a/src/path-seat.c
+++ b/src/path-seat.c
@@ -83,8 +83,6 @@ path_seat_create(struct path_input *input,
 	struct path_seat *seat;
 
 	seat = zalloc(sizeof(*seat));
-	if (!seat)
-		return NULL;
 
 	libinput_seat_init(&seat->base, &input->base, seat_name,
 			   seat_logical_name, path_seat_destroy);
@@ -226,9 +224,6 @@ path_create_device(struct libinput *libinput,
 	struct libinput_device *device;
 
 	dev = zalloc(sizeof *dev);
-	if (!dev)
-		return NULL;
-
 	dev->udev_device = udev_device_ref(udev_device);
 
 	list_insert(&input->path_list, &dev->link);
@@ -285,8 +280,7 @@ libinput_path_create_context(const struct libinput_interface *interface,
 		return NULL;
 
 	input = zalloc(sizeof *input);
-	if (!input ||
-	    libinput_init(&input->base, interface,
+	if (libinput_init(&input->base, interface,
 			  &interface_backend, user_data) != 0) {
 		udev_unref(udev);
 		free(input);
diff --git a/src/udev-seat.c b/src/udev-seat.c
index 685b4486..b954e3ca 100644
--- a/src/udev-seat.c
+++ b/src/udev-seat.c
@@ -296,8 +296,6 @@ udev_seat_create(struct udev_input *input,
 	struct udev_seat *seat;
 
 	seat = zalloc(sizeof *seat);
-	if (!seat)
-		return NULL;
 
 	libinput_seat_init(&seat->base, &input->base,
 			   device_seat, seat_name,
@@ -355,8 +353,6 @@ libinput_udev_create_context(const struct libinput_interface *interface,
 		return NULL;
 
 	input = zalloc(sizeof *input);
-	if (!input)
-		return NULL;
 
 	if (libinput_init(&input->base, interface,
 			  &interface_backend, user_data) != 0) {
diff --git a/test/litest-device-atmel-hover.c b/test/litest-device-atmel-hover.c
index 942744e2..8b818da1 100644
--- a/test/litest-device-atmel-hover.c
+++ b/test/litest-device-atmel-hover.c
@@ -148,8 +148,9 @@ struct litest_test_device litest_atmel_hover_device = {
 static void
 atmel_hover_create(struct litest_device *d)
 {
-	struct litest_semi_mt *semi_mt = zalloc(sizeof(*semi_mt));
-	assert(semi_mt);
+	struct litest_semi_mt *semi_mt;
+
+	semi_mt = zalloc(sizeof(*semi_mt));
 
 	d->private = semi_mt;
 
diff --git a/test/litest.c b/test/litest.c
index abd72188..ead3371c 100644
--- a/test/litest.c
+++ b/test/litest.c
@@ -524,7 +524,6 @@ litest_add_tcase_for_device(struct suite *suite,
 	struct test *t;
 
 	t = zalloc(sizeof(*t));
-	assert(t != NULL);
 	t->name = strdup(funcname);
 	t->devname = strdup(dev->shortname);
 	t->func = func;
@@ -550,7 +549,6 @@ litest_add_tcase_no_device(struct suite *suite,
 		return;
 
 	t = zalloc(sizeof(*t));
-	assert(t != NULL);
 	t->name = strdup(test_name);
 	t->devname = strdup("no device");
 	t->func = func;
@@ -573,7 +571,6 @@ get_suite(const char *name)
 	}
 
 	s = zalloc(sizeof(*s));
-	assert(s != NULL);
 	s->name = strdup(name);
 
 	list_init(&s->tests);
@@ -831,7 +828,6 @@ litest_init_all_device_udev_rules(struct list *created_files)
 		udev_file = litest_init_device_udev_rules(*dev);
 		if (udev_file) {
 			struct created_file *file = zalloc(sizeof(*file));
-			litest_assert(file);
 			file->path = udev_file;
 			list_insert(created_files, &file->link);
 		}
@@ -952,7 +948,6 @@ litest_run_suite(struct list *tests, int which, int max)
 				  t->devname);
 			litest_assert(sname != NULL);
 			n = zalloc(sizeof(*n));
-			litest_assert_notnull(n);
 			n->name = sname;
 			list_insert(&testnames, &n->node);
 
@@ -962,7 +957,6 @@ litest_run_suite(struct list *tests, int which, int max)
 				  t->devname);
 			litest_assert(tname != NULL);
 			n = zalloc(sizeof(*n));
-			litest_assert_notnull(n);
 			n->name = tname;
 			list_insert(&testnames, &n->node);
 
@@ -1139,7 +1133,6 @@ litest_copy_file(const char *dest, const char *src, const char *header)
 	int suffixlen;
 
 	file = zalloc(sizeof(*file));
-	litest_assert(file);
 	file->path = strdup(dest);
 	litest_assert(file->path);
 
@@ -1295,7 +1288,6 @@ litest_create(enum litest_device_type which,
 		ck_abort_msg("Invalid device type %d\n", which);
 
 	d = zalloc(sizeof(*d));
-	litest_assert(d != NULL);
 
 	/* device has custom create method */
 	if ((*dev)->create) {
diff --git a/tools/libinput-measure-touchpad-tap.c b/tools/libinput-measure-touchpad-tap.c
index 0b2b195f..ca361617 100644
--- a/tools/libinput-measure-touchpad-tap.c
+++ b/tools/libinput-measure-touchpad-tap.c
@@ -71,8 +71,9 @@ touch_tdelta_ms(const struct touch *t)
 static inline struct tap_data *
 tap_data_new(void)
 {
-	struct tap_data *tap_data = zalloc(sizeof(struct tap_data));
-	assert(tap_data);
+	struct tap_data *tap_data;
+
+	tap_data = zalloc(sizeof(struct tap_data));
 
 	return tap_data;
 }
@@ -122,7 +123,6 @@ tap_data_duplicate_sorted(const struct tap_data *src,
 	dest->toffset = src->toffset;
 	dest->touches_sz = dest->count;
 	dest->touches = zalloc(dest->count * sizeof(*dest->touches));
-	assert(dest->touches);
 
 	memcpy(dest->touches,
 	       src->touches,
-- 
2.13.0



More information about the wayland-devel mailing list