From 38839ac536524e0bb43eaea80cf02b57463a59e3 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 6 Mar 2023 13:23:51 +0100 Subject: [PATCH] gamma-control-v1: simplify get_gamma_control error handling Allocate resources only after checking the request. --- types/wlr_gamma_control_v1.c | 60 ++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/types/wlr_gamma_control_v1.c b/types/wlr_gamma_control_v1.c index 4c1e1f1e..b38408d3 100644 --- a/types/wlr_gamma_control_v1.c +++ b/types/wlr_gamma_control_v1.c @@ -171,61 +171,55 @@ static void gamma_control_manager_get_gamma_control(struct wl_client *client, gamma_control_manager_from_resource(manager_resource); struct wlr_output *output = wlr_output_from_resource(output_resource); - struct wlr_gamma_control_v1 *gamma_control = - calloc(1, sizeof(struct wlr_gamma_control_v1)); - if (gamma_control == NULL) { - wl_client_post_no_memory(client); - return; - } - gamma_control->output = output; - uint32_t version = wl_resource_get_version(manager_resource); - gamma_control->resource = wl_resource_create(client, + struct wl_resource *resource = wl_resource_create(client, &zwlr_gamma_control_v1_interface, version, id); - if (gamma_control->resource == NULL) { - free(gamma_control); + if (resource == NULL) { wl_client_post_no_memory(client); return; } - wl_resource_set_implementation(gamma_control->resource, &gamma_control_impl, - gamma_control, gamma_control_handle_resource_destroy); + wl_resource_set_implementation(resource, &gamma_control_impl, + NULL, gamma_control_handle_resource_destroy); if (output == NULL) { - wl_resource_set_user_data(gamma_control->resource, NULL); - zwlr_gamma_control_v1_send_failed(gamma_control->resource); - free(gamma_control); + zwlr_gamma_control_v1_send_failed(resource); return; } - wl_signal_add(&output->events.destroy, - &gamma_control->output_destroy_listener); - gamma_control->output_destroy_listener.notify = - gamma_control_handle_output_destroy; - - wl_signal_add(&output->events.commit, - &gamma_control->output_commit_listener); - gamma_control->output_commit_listener.notify = - gamma_control_handle_output_commit; - - wl_list_init(&gamma_control->link); - size_t gamma_size = wlr_output_get_gamma_size(output); if (gamma_size == 0) { - zwlr_gamma_control_v1_send_failed(gamma_control->resource); - gamma_control_destroy(gamma_control); + zwlr_gamma_control_v1_send_failed(resource); return; } struct wlr_gamma_control_v1 *gc; wl_list_for_each(gc, &manager->controls, link) { if (gc->output == output) { - zwlr_gamma_control_v1_send_failed(gamma_control->resource); - gamma_control_destroy(gamma_control); + zwlr_gamma_control_v1_send_failed(resource); return; } } - wl_list_remove(&gamma_control->link); + struct wlr_gamma_control_v1 *gamma_control = + calloc(1, sizeof(struct wlr_gamma_control_v1)); + if (gamma_control == NULL) { + wl_client_post_no_memory(client); + return; + } + gamma_control->output = output; + gamma_control->resource = resource; + wl_resource_set_user_data(resource, gamma_control); + + wl_signal_add(&output->events.destroy, + &gamma_control->output_destroy_listener); + gamma_control->output_destroy_listener.notify = + gamma_control_handle_output_destroy; + + wl_signal_add(&output->events.commit, + &gamma_control->output_commit_listener); + gamma_control->output_commit_listener.notify = + gamma_control_handle_output_commit; + wl_list_insert(&manager->controls, &gamma_control->link); zwlr_gamma_control_v1_send_gamma_size(gamma_control->resource, gamma_size); }