We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3720

Summary: SDL_GL_GetAttribute doesn't check for initialized video driver
Product: SDL Reporter: Simon Hug <chli.hug>
Component: videoAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: HG 2.0   
Hardware: All   
OS: All   
Attachments: Patch that fixes a possible NULL-pointer indirection in SDL_GL_GetAttribute.

Description Simon Hug 2017-07-31 15:01:49 UTC
Created attachment 2813 [details]
Patch that fixes a possible NULL-pointer indirection in SDL_GL_GetAttribute.

SDL_GL_GetAttribute doesn't check if a video driver has been initialized and will access the SDL_VideoDevice pointer, which is NULL at that point.

I think all of the attributes require an initialized driver, so a simple NULL check should fix it. Patch is attached.

Test case:

#include <SDL.h>

int main(int argc, char*argv[])
{
    int val, res;

    res = SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &val);
    if (res != 0)
        SDL_Log("Error getting attribute before init (%d): %s\n", res, SDL_GetError());
    else
        SDL_Log("Major version set to %d\n", val);

    res = SDL_Init(SDL_INIT_VIDEO);
    if (res != 0)
        SDL_Log("Error initializing SDL (%d): %s\n", res, SDL_GetError());

    res = SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &val);
    if (res != 0)
        SDL_Log("Error getting attribute after init (%d): %s\n", res, SDL_GetError());
    else
        SDL_Log("Major version set to %d\n", val);

    SDL_Quit();
    return 0;
}
Comment 1 Sam Lantinga 2017-07-31 19:57:29 UTC
Fixed, thanks!
https://hg.libsdl.org/SDL/rev/6f6e8880cd37