From aa259ed52d03864e1d8ee2f1c5a4227db15c75ed Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Thu, 28 May 2020 15:18:41 -0400 Subject: [PATCH] wayland: Changed output removal in handle_surface_leave() No longer needs an extra malloc, handles unexpected cases like the same output being listed twice. --- src/video/wayland/SDL_waylandwindow.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/video/wayland/SDL_waylandwindow.c b/src/video/wayland/SDL_waylandwindow.c index 830faba78..b5dbbd0d1 100644 --- a/src/video/wayland/SDL_waylandwindow.c +++ b/src/video/wayland/SDL_waylandwindow.c @@ -406,21 +406,22 @@ static void handle_surface_leave(void *data, struct wl_surface *surface, struct wl_output *output) { SDL_WindowData *window = data; + int num_outputs = 0; int i; - if (window->num_outputs > 1) { - struct wl_output **new_outputs = SDL_malloc((window->num_outputs - 1) * sizeof *window->outputs), **iter = new_outputs; - for (i=0; i < window->num_outputs; i++) { - if (window->outputs[i] != output) { - *iter = window->outputs[i]; - iter++; - } - } - SDL_free(window->outputs); - window->outputs = new_outputs; - window->num_outputs--; - } else { - window->num_outputs = 0; + for (i = 0; i < window->num_outputs; i++) { + if (window->outputs[i] == output) { /* remove this one */ + if (i == (window->num_outputs-1)) { + window->outputs[i] = NULL; + } else { + SDL_memmove(&window->outputs[i], &window->outputs[i+1], sizeof (output) * ((window->num_outputs - i) - 1)); + } + window->num_outputs--; + i--; + } + } + + if (window->num_outputs == 0) { SDL_free(window->outputs); window->outputs = NULL; }