From 7cb206deb4e24847297e69859488d2e24445d9e2 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Mon, 8 Apr 2024 10:11:50 -0400 Subject: [PATCH 1/7] examples: assert on memory allocation Also checks for NULL output in the `X_from_device` functions --- examples/cairo-buffer.c | 3 ++- examples/embedded.c | 2 ++ examples/fullscreen-shell.c | 2 ++ examples/output-layers.c | 3 +++ examples/output-layout.c | 4 ++++ examples/pointer.c | 4 ++++ examples/rotation.c | 4 ++++ examples/scene-graph.c | 2 ++ examples/simple.c | 4 ++++ examples/tablet.c | 7 +++++++ examples/touch.c | 5 +++++ 11 files changed, 39 insertions(+), 1 deletion(-) diff --git a/examples/cairo-buffer.c b/examples/cairo-buffer.c index 7de4a053b..1058774bf 100644 --- a/examples/cairo-buffer.c +++ b/examples/cairo-buffer.c @@ -1,4 +1,4 @@ - +#include #include #include #include @@ -101,6 +101,7 @@ static void server_handle_new_output(struct wl_listener *listener, void *data) { wlr_output_init_render(wlr_output, server->allocator, server->renderer); struct output *output = calloc(1, sizeof(*output)); + assert(output); output->wlr = wlr_output; output->server = server; output->frame.notify = output_handle_frame; diff --git a/examples/embedded.c b/examples/embedded.c index f0f661198..9edf9577b 100644 --- a/examples/embedded.c +++ b/examples/embedded.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -145,6 +146,7 @@ static void handle_new_surface(struct wl_listener *listener, void *data) { struct wlr_surface *wlr_surface = data; struct surface *surface = calloc(1, sizeof(*surface)); + assert(surface); surface->wlr = wlr_surface; surface->commit.notify = surface_handle_commit; diff --git a/examples/fullscreen-shell.c b/examples/fullscreen-shell.c index c12f126e9..bb4261895 100644 --- a/examples/fullscreen-shell.c +++ b/examples/fullscreen-shell.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -154,6 +155,7 @@ static void server_handle_new_output(struct wl_listener *listener, void *data) { wlr_output_init_render(wlr_output, server->allocator, server->renderer); struct fullscreen_output *output = calloc(1, sizeof(*output)); + assert(output); output->wlr_output = wlr_output; output->server = server; output->frame.notify = output_handle_frame; diff --git a/examples/output-layers.c b/examples/output-layers.c index 5089c6aa5..0ee038dd3 100644 --- a/examples/output-layers.c +++ b/examples/output-layers.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -166,6 +167,7 @@ static void server_handle_new_output(struct wl_listener *listener, void *data) { wlr_output_init_render(wlr_output, server->allocator, server->renderer); struct output *output = calloc(1, sizeof(*output)); + assert(output); output->wlr_output = wlr_output; output->server = server; wl_list_init(&output->surfaces); @@ -247,6 +249,7 @@ static void server_handle_new_surface(struct wl_listener *listener, struct output *output; wl_list_for_each(output, &server->outputs, link) { struct output_surface *output_surface = calloc(1, sizeof(*output_surface)); + assert(output_surface); output_surface->wlr_surface = wlr_surface; output_surface->server = server; output_surface->destroy.notify = output_surface_handle_destroy; diff --git a/examples/output-layout.c b/examples/output-layout.c index 3a3eeb24f..13f572cd9 100644 --- a/examples/output-layout.c +++ b/examples/output-layout.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -167,6 +168,7 @@ static void new_output_notify(struct wl_listener *listener, void *data) { wlr_output_init_render(output, sample->allocator, sample->renderer); struct sample_output *sample_output = calloc(1, sizeof(*sample_output)); + assert(sample_output); wlr_output_layout_add_auto(sample->layout, output); sample_output->output = output; sample_output->sample = sample; @@ -235,7 +237,9 @@ static void new_input_notify(struct wl_listener *listener, void *data) { switch (device->type) { case WLR_INPUT_DEVICE_KEYBOARD:; struct sample_keyboard *keyboard = calloc(1, sizeof(*keyboard)); + assert(keyboard); keyboard->wlr_keyboard = wlr_keyboard_from_input_device(device); + assert(keyboard->wlr_keyboard); keyboard->sample = sample; wl_signal_add(&device->events.destroy, &keyboard->destroy); keyboard->destroy.notify = keyboard_destroy_notify; diff --git a/examples/pointer.c b/examples/pointer.c index fed37a5b3..5b0de644b 100644 --- a/examples/pointer.c +++ b/examples/pointer.c @@ -193,6 +193,7 @@ static void handle_touch_down(struct wl_listener *listener, void *data) { struct sample_state *sample = wl_container_of(listener, sample, touch_down); struct wlr_touch_down_event *event = data; struct touch_point *point = calloc(1, sizeof(*point)); + assert(point); point->touch_id = event->touch_id; point->x = event->x; point->y = event->y; @@ -265,6 +266,7 @@ static void new_output_notify(struct wl_listener *listener, void *data) { wlr_output_init_render(output, sample->allocator, sample->renderer); struct sample_output *sample_output = calloc(1, sizeof(*sample_output)); + assert(sample_output); sample_output->output = output; sample_output->state = sample; wl_signal_add(&output->events.frame, &sample_output->frame); @@ -304,7 +306,9 @@ static void new_input_notify(struct wl_listener *listener, void *data) { case WLR_INPUT_DEVICE_KEYBOARD:; struct sample_keyboard *keyboard = calloc(1, sizeof(*keyboard)); + assert(keyboard); keyboard->wlr_keyboard = wlr_keyboard_from_input_device(device); + assert(keyboard->wlr_keyboard); keyboard->state = state; wl_signal_add(&device->events.destroy, &keyboard->destroy); keyboard->destroy.notify = keyboard_destroy_notify; diff --git a/examples/rotation.c b/examples/rotation.c index 351973f61..b4b1d3145 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -118,6 +119,7 @@ static void new_output_notify(struct wl_listener *listener, void *data) { wlr_output_init_render(output, sample->allocator, sample->renderer); struct sample_output *sample_output = calloc(1, sizeof(*sample_output)); + assert(sample_output); sample_output->x_offs = sample_output->y_offs = 0; sample_output->x_vel = sample_output->y_vel = 128; @@ -186,7 +188,9 @@ static void new_input_notify(struct wl_listener *listener, void *data) { switch (device->type) { case WLR_INPUT_DEVICE_KEYBOARD:; struct sample_keyboard *keyboard = calloc(1, sizeof(*keyboard)); + assert(keyboard); keyboard->wlr_keyboard = wlr_keyboard_from_input_device(device); + assert(keyboard->wlr_keyboard); keyboard->sample = sample; wl_signal_add(&device->events.destroy, &keyboard->destroy); keyboard->destroy.notify = keyboard_destroy_notify; diff --git a/examples/scene-graph.c b/examples/scene-graph.c index 421986534..7f2dc65d7 100644 --- a/examples/scene-graph.c +++ b/examples/scene-graph.c @@ -72,6 +72,7 @@ static void server_handle_new_output(struct wl_listener *listener, void *data) { wlr_output_init_render(wlr_output, server->allocator, server->renderer); struct output *output = calloc(1, sizeof(*output)); + assert(output); output->wlr = wlr_output; output->server = server; output->frame.notify = output_handle_frame; @@ -124,6 +125,7 @@ static void server_handle_new_surface(struct wl_listener *listener, server->surface_offset += 50; struct surface *surface = calloc(1, sizeof(*surface)); + assert(surface); surface->wlr = wlr_surface; surface->commit.notify = surface_handle_commit; wl_signal_add(&wlr_surface->events.commit, &surface->commit); diff --git a/examples/simple.c b/examples/simple.c index 81ed2e0e9..32f80fec8 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -97,6 +98,7 @@ static void new_output_notify(struct wl_listener *listener, void *data) { wlr_output_init_render(output, sample->allocator, sample->renderer); struct sample_output *sample_output = calloc(1, sizeof(*sample_output)); + assert(sample_output); sample_output->output = output; sample_output->sample = sample; wl_signal_add(&output->events.frame, &sample_output->frame); @@ -145,7 +147,9 @@ static void new_input_notify(struct wl_listener *listener, void *data) { switch (device->type) { case WLR_INPUT_DEVICE_KEYBOARD:; struct sample_keyboard *keyboard = calloc(1, sizeof(*keyboard)); + assert(keyboard); keyboard->wlr_keyboard = wlr_keyboard_from_input_device(device); + assert(keyboard->wlr_keyboard); keyboard->sample = sample; wl_signal_add(&device->events.destroy, &keyboard->destroy); keyboard->destroy.notify = keyboard_destroy_notify; diff --git a/examples/tablet.c b/examples/tablet.c index 8f0754df1..19434b1dc 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -1,5 +1,6 @@ #undef _POSIX_C_SOURCE #define _XOPEN_SOURCE 600 // for M_PI +#include #include #include #include @@ -271,6 +272,7 @@ static void new_output_notify(struct wl_listener *listener, void *data) { wlr_output_init_render(output, sample->allocator, sample->renderer); struct sample_output *sample_output = calloc(1, sizeof(*sample_output)); + assert(sample_output); sample_output->output = output; sample_output->sample = sample; wl_signal_add(&output->events.frame, &sample_output->frame); @@ -318,7 +320,9 @@ static void new_input_notify(struct wl_listener *listener, void *data) { switch (device->type) { case WLR_INPUT_DEVICE_KEYBOARD:; struct sample_keyboard *keyboard = calloc(1, sizeof(*keyboard)); + assert(keyboard); keyboard->wlr_keyboard = wlr_keyboard_from_input_device(device); + assert(keyboard->wlr_keyboard); keyboard->sample = sample; wl_signal_add(&device->events.destroy, &keyboard->destroy); keyboard->destroy.notify = keyboard_destroy_notify; @@ -341,7 +345,9 @@ static void new_input_notify(struct wl_listener *listener, void *data) { break; case WLR_INPUT_DEVICE_TABLET_PAD:; struct tablet_pad_state *pstate = calloc(1, sizeof(*pstate)); + assert(pstate); pstate->wlr_tablet_pad = wlr_tablet_pad_from_input_device(device); + assert(pstate->wlr_tablet_pad); pstate->sample = sample; pstate->destroy.notify = tablet_pad_destroy_notify; wl_signal_add(&device->events.destroy, &pstate->destroy); @@ -359,6 +365,7 @@ static void new_input_notify(struct wl_listener *listener, void *data) { 10 : tablet->height_mm; struct tablet_tool_state *tstate = calloc(1, sizeof(*tstate)); + assert(tstate); tstate->wlr_tablet = tablet; tstate->sample = sample; tstate->destroy.notify = tablet_tool_destroy_notify; diff --git a/examples/touch.c b/examples/touch.c index e1bd73818..845d883d7 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -103,6 +104,7 @@ static void touch_down_notify(struct wl_listener *listener, void *data) { struct touch_state *tstate = wl_container_of(listener, tstate, down); struct sample_state *sample = tstate->sample; struct touch_point *point = calloc(1, sizeof(*point)); + assert(point); point->touch_id = event->touch_id; point->x = event->x; point->y = event->y; @@ -174,6 +176,7 @@ static void new_output_notify(struct wl_listener *listener, void *data) { wlr_output_init_render(output, sample->allocator, sample->renderer); struct sample_output *sample_output = calloc(1, sizeof(*sample_output)); + assert(sample_output); sample_output->output = output; sample_output->sample = sample; wl_signal_add(&output->events.frame, &sample_output->frame); @@ -221,6 +224,7 @@ static void new_input_notify(struct wl_listener *listener, void *data) { switch (device->type) { case WLR_INPUT_DEVICE_KEYBOARD:; struct sample_keyboard *keyboard = calloc(1, sizeof(*keyboard)); + assert(keyboard); keyboard->wlr_keyboard = wlr_keyboard_from_input_device(device); keyboard->sample = sample; wl_signal_add(&device->events.destroy, &keyboard->destroy); @@ -244,6 +248,7 @@ static void new_input_notify(struct wl_listener *listener, void *data) { break; case WLR_INPUT_DEVICE_TOUCH:; struct touch_state *tstate = calloc(1, sizeof(*tstate)); + assert(tstate); tstate->wlr_touch = wlr_touch_from_input_device(device); tstate->sample = sample; tstate->destroy.notify = touch_destroy_notify; From b18aec36253c7864d5c413d684a8d665b5b81640 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Mon, 8 Apr 2024 10:12:14 -0400 Subject: [PATCH 2/7] tinywl: assert on memory allocation --- tinywl/tinywl.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tinywl/tinywl.c b/tinywl/tinywl.c index 6c043ed51..ead06fcf8 100644 --- a/tinywl/tinywl.c +++ b/tinywl/tinywl.c @@ -250,6 +250,7 @@ static void server_new_keyboard(struct tinywl_server *server, struct wlr_keyboard *wlr_keyboard = wlr_keyboard_from_input_device(device); struct tinywl_keyboard *keyboard = calloc(1, sizeof(*keyboard)); + assert(keyboard); keyboard->server = server; keyboard->wlr_keyboard = wlr_keyboard; @@ -625,6 +626,7 @@ static void server_new_output(struct wl_listener *listener, void *data) { /* Allocates and configures our state for this output */ struct tinywl_output *output = calloc(1, sizeof(*output)); + assert(output); output->wlr_output = wlr_output; output->server = server; @@ -801,6 +803,7 @@ static void server_new_xdg_toplevel(struct wl_listener *listener, void *data) { /* Allocate a tinywl_toplevel for this surface */ struct tinywl_toplevel *toplevel = calloc(1, sizeof(*toplevel)); + assert(toplevel); toplevel->server = server; toplevel->xdg_toplevel = xdg_toplevel; toplevel->scene_tree = @@ -859,6 +862,7 @@ static void server_new_xdg_popup(struct wl_listener *listener, void *data) { struct wlr_xdg_popup *xdg_popup = data; struct tinywl_popup *popup = calloc(1, sizeof(*popup)); + assert(popup); popup->xdg_popup = xdg_popup; /* We must add xdg popups to the scene graph so they get rendered. The From a79f7096a67daf94dffb1eace683a06119827886 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Thu, 18 Apr 2024 13:22:37 -0400 Subject: [PATCH 3/7] types/scene: assert scene surface in scene_buffer_point_accepts_input --- types/scene/surface.c | 1 + 1 file changed, 1 insertion(+) diff --git a/types/scene/surface.c b/types/scene/surface.c index 11396e2fd..9d1e5d62d 100644 --- a/types/scene/surface.c +++ b/types/scene/surface.c @@ -188,6 +188,7 @@ static bool scene_buffer_point_accepts_input(struct wlr_scene_buffer *scene_buff double *sx, double *sy) { struct wlr_scene_surface *scene_surface = wlr_scene_surface_try_from_buffer(scene_buffer); + assert(scene_surface); *sx += scene_surface->clip.x; *sy += scene_surface->clip.y; From 140197f56d100c6c41d60ad45c514999fb0bab42 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Thu, 18 Apr 2024 13:23:10 -0400 Subject: [PATCH 4/7] backend/libinput: assert buffer overflow in add_pad_group_from_libinput --- backend/libinput/tablet_pad.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/backend/libinput/tablet_pad.c b/backend/libinput/tablet_pad.c index 98b3e4f4b..b4e1b7286 100644 --- a/backend/libinput/tablet_pad.c +++ b/backend/libinput/tablet_pad.c @@ -40,6 +40,7 @@ static void add_pad_group_from_libinput(struct wlr_tablet_pad *pad, size_t ring = 0; for (size_t i = 0; i < pad->ring_count; ++i) { if (libinput_tablet_pad_mode_group_has_ring(li_group, i)) { + assert(ring < group->ring_count); group->rings[ring++] = i; } } @@ -53,9 +54,11 @@ static void add_pad_group_from_libinput(struct wlr_tablet_pad *pad, if (group->strips == NULL) { goto group_fail; } + size_t strip = 0; for (size_t i = 0; i < pad->strip_count; ++i) { if (libinput_tablet_pad_mode_group_has_strip(li_group, i)) { + assert(strip < group->strip_count); group->strips[strip++] = i; } } @@ -69,9 +72,11 @@ static void add_pad_group_from_libinput(struct wlr_tablet_pad *pad, if (group->buttons == NULL) { goto group_fail; } + size_t button = 0; for (size_t i = 0; i < pad->button_count; ++i) { if (libinput_tablet_pad_mode_group_has_button(li_group, i)) { + assert(button < group->button_count); group->buttons[button++] = i; } } From 40fe1ce2c7669b470cdbafa082cb893ebbd10693 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Thu, 18 Apr 2024 13:26:12 -0400 Subject: [PATCH 5/7] backend/session: improve explicit_gpu_find device handling --- backend/session/session.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/session/session.c b/backend/session/session.c index 5fb20c226..2a3317f47 100644 --- a/backend/session/session.c +++ b/backend/session/session.c @@ -402,11 +402,11 @@ static ssize_t explicit_find_gpus(struct wlr_session *session, break; } - ret[i] = session_open_if_kms(session, ptr); - if (!ret[i]) { - wlr_log(WLR_ERROR, "Unable to open %s as DRM device", ptr); + struct wlr_device *dev = session_open_if_kms(session, ptr); + if (dev) { + ret[i++] = dev; } else { - ++i; + wlr_log(WLR_ERROR, "Unable to open %s as DRM device", ptr); } } while ((ptr = strtok_r(NULL, ":", &save))); From 9cf810c3fb060590a924b12f0ce340e994079202 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Thu, 18 Apr 2024 13:27:03 -0400 Subject: [PATCH 6/7] backend: close session file on drm backend creation failure --- backend/backend.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backend/backend.c b/backend/backend.c index e4e8c8d8e..6df7433fe 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -260,6 +260,8 @@ static struct wlr_backend *attempt_drm_backend(struct wlr_backend *backend, stru for (size_t i = 0; i < (size_t)num_gpus; ++i) { struct wlr_backend *drm = wlr_drm_backend_create(session, gpus[i], primary_drm); if (!drm) { + wlr_session_close_file(session, gpus[i]); + wlr_log(WLR_ERROR, "Failed to create DRM backend"); continue; } @@ -270,6 +272,7 @@ static struct wlr_backend *attempt_drm_backend(struct wlr_backend *backend, stru wlr_multi_backend_add(backend, drm); } + if (!primary_drm) { wlr_log(WLR_ERROR, "Could not successfully create backend on any GPU"); return NULL; From f55808acebdc67b74efe28c8790cf059bba32b68 Mon Sep 17 00:00:00 2001 From: Simon Zeni Date: Thu, 18 Apr 2024 13:27:28 -0400 Subject: [PATCH 7/7] backend/wayland: fix tablet_v2 group buttons allocation --- backend/wayland/tablet_v2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/wayland/tablet_v2.c b/backend/wayland/tablet_v2.c index 53e7c9148..ac6ac1d49 100644 --- a/backend/wayland/tablet_v2.c +++ b/backend/wayland/tablet_v2.c @@ -184,7 +184,7 @@ static void handle_tablet_pad_group_buttons(void *data, struct tablet_pad_group *group = data; free(group->group.buttons); - group->group.buttons = calloc(1, buttons->size); + group->group.buttons = malloc(buttons->size); if (!group->group.buttons) { // FIXME: Add actual error handling return;