Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinguish between owned and borrowed references to a SDL_Surface #306

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

smcv
Copy link

@smcv smcv commented Jul 18, 2023

In many SDL APIs that return a SDL_Surface *, the surface is considered to be owned by the caller, and must be freed by the caller.

However, SDL_SetVideoMode and presumably SDL_GetVideoSurface return a pointer to SDL's internal video surface, which will be freed by SDL if necessary, and must not be freed by library users. Incorrectly freeing this surface can lead to a use-after-free crash, manifesting as a test failure in t/core_video.t.

See also libsdl-org/sdl12-compat#305

Resolves: #305


This could probably be done a lot more elegantly, but it's my first attempt at writing XS.

In many SDL APIs that return a SDL_Surface *, the surface is considered
to be owned by the caller, and must be freed by the caller.

However, SDL_SetVideoMode and presumably SDL_GetVideoSurface return
a pointer to SDL's internal video surface, which will be freed by SDL
if necessary, and must not be freed by library users.
Incorrectly freeing this surface can lead to a use-after-free crash,
manifesting as a test failure in t/core_video.t.

See also libsdl-org/sdl12-compat#305

Resolves: PerlGameDev#305
Signed-off-by: Simon McVittie <[email protected]>
@smcv smcv marked this pull request as draft July 18, 2023 17:03
@smcv smcv force-pushed the borrowed-surfaces branch from 5ffbcd0 to e9b907c Compare July 18, 2023 17:04
@smcv smcv marked this pull request as ready for review July 18, 2023 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tries to free previous results of SDL_SetVideoMode(), leading to use-after-free crash
1 participant