diff options
author | Kirill Primak <vyivel@eclair.cafe> | 2024-02-13 19:19:41 +0300 |
---|---|---|
committer | Kirill Primak <vyivel@eclair.cafe> | 2024-02-13 19:36:21 +0300 |
commit | 4c69bc47f4b80fe834683d59d2aeca6ad7de38d8 (patch) | |
tree | 941781fff3c1b8dd67741a53f681b80e16ec6e37 | |
parent | 6ad9e89a3423f3bb94711c646335b8dbe32685b9 (diff) |
ext-foreign-toplevel-list-v1: improve/fix update_state()
This extracts common string updating logic into a function and fixes a
possible NULL dereference.
-rw-r--r-- | types/wlr_ext_foreign_toplevel_list_v1.c | 59 |
1 files changed, 25 insertions, 34 deletions
diff --git a/types/wlr_ext_foreign_toplevel_list_v1.c b/types/wlr_ext_foreign_toplevel_list_v1.c index 326e4f41..94713183 100644 --- a/types/wlr_ext_foreign_toplevel_list_v1.c +++ b/types/wlr_ext_foreign_toplevel_list_v1.c @@ -24,47 +24,38 @@ static const struct ext_foreign_toplevel_list_v1_interface toplevel_handle_impl .destroy = foreign_toplevel_handle_destroy, }; -void wlr_ext_foreign_toplevel_handle_v1_update_state( - struct wlr_ext_foreign_toplevel_handle_v1 *toplevel, - const struct wlr_ext_foreign_toplevel_handle_v1_state *state) { - bool changed_app_id = false; - bool changed_title = false; - - if (state->app_id) { - if (strcmp(toplevel->app_id, state->app_id) != 0) { - free(toplevel->app_id); - toplevel->app_id = strdup(state->app_id); - if (toplevel->app_id == NULL) { - wlr_log(WLR_ERROR, "failed to allocate memory for toplevel app_id"); - return; - } - changed_app_id = true; - } - } else { - if (toplevel->app_id) { - free(toplevel->app_id); - toplevel->app_id = NULL; - changed_app_id = true; +// Returns true if clients need to be notified about the update +static bool update_string(struct wlr_ext_foreign_toplevel_handle_v1 *toplevel, + char **dst, const char *src) { + if (src == NULL) { + if (*dst == NULL) { + return false; } + } else if (*dst != NULL && strcmp(*dst, src) == 0) { + return false; } - if (state->title) { - if (strcmp(toplevel->title, state->title) != 0) { - free(toplevel->title); - toplevel->title = strdup(state->title); - if (toplevel->title == NULL) { - wlr_log(WLR_ERROR, "failed to allocate memory for toplevel title"); - return; + free(*dst); + if (src != NULL) { + *dst = strdup(src); + if (*dst == NULL) { + struct wl_resource *resource; + wl_resource_for_each(resource, &toplevel->resources) { + wl_resource_post_no_memory(resource); } - changed_title = true; + return false; } } else { - if (toplevel->title) { - free(toplevel->title); - toplevel->title = NULL; - changed_title = true; - } + *dst = NULL; } + return true; +} + +void wlr_ext_foreign_toplevel_handle_v1_update_state( + struct wlr_ext_foreign_toplevel_handle_v1 *toplevel, + const struct wlr_ext_foreign_toplevel_handle_v1_state *state) { + bool changed_app_id = update_string(toplevel, &toplevel->app_id, state->app_id); + bool changed_title = update_string(toplevel, &toplevel->title, state->title); if (!changed_app_id && !changed_title) { return; |