Fix mode-pointer lifetime bugs and assign mode ids at enumeration - #105
Open
poliva wants to merge 1 commit into
Open
Fix mode-pointer lifetime bugs and assign mode ids at enumeration#105poliva wants to merge 1 commit into
poliva wants to merge 1 commit into
Conversation
Owner
|
Hi poliva, Thanks a lot for your PR and for detecting these bugs. I'm just not merging it directly because I have 2 concerns:
I need some time to think about this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I am integrating switchres into Fightcade's fork of FBNeo for an upcoming Fightcde update and found several related bugs around current_mode and mode ids, while saving st.current_mode at init and tried to restore it later.
get_available_video_modes() stored the address of the loop-local
modevia set_current_mode(&mode). That stack slot is reclaimed when the function returns, so current_mode() then returned a dangling pointer and sr_get_state() read current_mode()->id off the freed stack (valgrind: "Invalid read of size 4 ... below stack pointer" in sr_get_state).Point it at the stable desktop_mode member instead, same desktop-mode content, but it lives for the display's lifetime, which is what restore_desktop_mode() already relies on. Fixed in all three backends (linux, sdl2, windows).
xrandr_timing::set_timing read crtc_info->mode in the return expression after XRRFreeCrtcInfo(crtc_info). Capture the flag before freeing.
Mode ids were only assigned lazily in display_manager::get_mode() when a mode was selected, so every enumerated mode, including the desktop mode, sat at id 0. A frontend restoring with sr_set_mode(id) searched for id 0 and matched whatever mode happened to be first (e.g. a 240x192 CRT Emudriver mode) instead of the desktop mode.
Assign a unique id to every mode at enumeration time, before the desktop_mode/backup_modes copies are taken so all copies agree (windows, linux, sdl2).
Also fix sr_mode_internal's SR_ACTION_GET_FROM_ID lookup iterating video_modes by value: it stored the address of the loop-local copy as selected_mode, already dead when SR_ACTION_SWITCH dereferenced it.