Merge pull request #2117 from psychon/invalid_screen_swap

screen:swap(): Avoid segfault on almost-deleted screens
This commit is contained in:
Daniel Hahler 2017-11-20 00:38:11 +01:00 committed by GitHub
commit 1326ec20fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 6 deletions

View File

@ -640,6 +640,7 @@ screen_refresh(void)
globalconf.screen_need_refresh = false; globalconf.screen_need_refresh = false;
screen_array_t new_screens; screen_array_t new_screens;
screen_array_t removed_screens;
lua_State *L = globalconf_get_lua_State(); lua_State *L = globalconf_get_lua_State();
bool list_changed = false; bool list_changed = false;
@ -669,6 +670,7 @@ screen_refresh(void)
} }
/* Remove screens which are gone */ /* Remove screens which are gone */
screen_array_init(&removed_screens);
for(int i = 0; i < globalconf.screens.len; i++) { for(int i = 0; i < globalconf.screens.len; i++) {
screen_t *old_screen = globalconf.screens.tab[i]; screen_t *old_screen = globalconf.screens.tab[i];
bool found = false; bool found = false;
@ -678,15 +680,18 @@ screen_refresh(void)
screen_array_take(&globalconf.screens, i); screen_array_take(&globalconf.screens, i);
i--; i--;
luaA_object_push(L, old_screen); screen_array_append(&removed_screens, old_screen);
screen_removed(L, -1);
lua_pop(L, 1);
luaA_object_unref(L, old_screen);
old_screen->valid = false;
list_changed = true; list_changed = true;
} }
} }
foreach(old_screen, removed_screens) {
luaA_object_push(L, *old_screen);
screen_removed(L, -1);
lua_pop(L, 1);
(*old_screen)->valid = false;
luaA_object_unref(L, *old_screen);
}
screen_array_wipe(&removed_screens);
/* Update changed screens */ /* Update changed screens */
foreach(existing_screen, globalconf.screens) foreach(existing_screen, globalconf.screens)
@ -1222,6 +1227,9 @@ luaA_screen_swap(lua_State *L)
if(ref_s && ref_swap) if(ref_s && ref_swap)
break; break;
} }
if(!ref_s || !ref_swap)
return luaL_error(L, "Invalid call to screen:swap()");
/* swap ! */ /* swap ! */
*ref_s = swap; *ref_s = swap;
*ref_swap = s; *ref_swap = s;