ext-foreign-toplevel-list-v1: improve/fix update_state()

This extracts common string updating logic into a function and fixes a
possible NULL dereference.
master
Kirill Primak 9 months ago
parent 6ad9e89a34
commit 4c69bc47f4

@ -24,47 +24,38 @@ static const struct ext_foreign_toplevel_list_v1_interface toplevel_handle_impl
.destroy = foreign_toplevel_handle_destroy, .destroy = foreign_toplevel_handle_destroy,
}; };
void wlr_ext_foreign_toplevel_handle_v1_update_state( // Returns true if clients need to be notified about the update
struct wlr_ext_foreign_toplevel_handle_v1 *toplevel, static bool update_string(struct wlr_ext_foreign_toplevel_handle_v1 *toplevel,
const struct wlr_ext_foreign_toplevel_handle_v1_state *state) { char **dst, const char *src) {
bool changed_app_id = false; if (src == NULL) {
bool changed_title = false; if (*dst == NULL) {
return 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;
} }
} else if (*dst != NULL && strcmp(*dst, src) == 0) {
return false;
} }
if (state->title) { free(*dst);
if (strcmp(toplevel->title, state->title) != 0) { if (src != NULL) {
free(toplevel->title); *dst = strdup(src);
toplevel->title = strdup(state->title); if (*dst == NULL) {
if (toplevel->title == NULL) { struct wl_resource *resource;
wlr_log(WLR_ERROR, "failed to allocate memory for toplevel title"); wl_resource_for_each(resource, &toplevel->resources) {
return; wl_resource_post_no_memory(resource);
} }
changed_title = true; return false;
} }
} else { } else {
if (toplevel->title) { *dst = NULL;
free(toplevel->title);
toplevel->title = NULL;
changed_title = true;
}
} }
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) { if (!changed_app_id && !changed_title) {
return; return;

Loading…
Cancel
Save