Fix crash when GL_LoadFunctions()/GLES2_LoadFunctions() fails
authorCameron Gutman <cameron.gutman@gmail.com>
Wed, 31 Oct 2018 20:17:53 -0700
changeset 1237784618d571795
parent 12376 cfc65d4d49ae
child 12378 61b267339239
Fix crash when GL_LoadFunctions()/GLES2_LoadFunctions() fails
https://bugzilla.libsdl.org/show_bug.cgi?id=4350

We can't safely call GL_DestroyRenderer() until GL_LoadFunctions()
succeeds because we will be missing functions that we try to use
when activating the renderer for destruction if we have an GL context.
src/render/opengl/SDL_render_gl.c
src/render/opengles2/SDL_render_gles2.c
     1.1 --- a/src/render/opengl/SDL_render_gl.c	Wed Oct 31 15:16:51 2018 -0700
     1.2 +++ b/src/render/opengl/SDL_render_gl.c	Wed Oct 31 20:17:53 2018 -0700
     1.3 @@ -420,7 +420,7 @@
     1.4  
     1.5      data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
     1.6      if (!data) {
     1.7 -        GL_DestroyRenderer(renderer);
     1.8 +        SDL_free(renderer);
     1.9          SDL_OutOfMemory();
    1.10          goto error;
    1.11      }
    1.12 @@ -455,16 +455,21 @@
    1.13  
    1.14      data->context = SDL_GL_CreateContext(window);
    1.15      if (!data->context) {
    1.16 -        GL_DestroyRenderer(renderer);
    1.17 +        SDL_free(renderer);
    1.18 +        SDL_free(data);
    1.19          goto error;
    1.20      }
    1.21      if (SDL_GL_MakeCurrent(window, data->context) < 0) {
    1.22 -        GL_DestroyRenderer(renderer);
    1.23 +        SDL_GL_DeleteContext(data->context);
    1.24 +        SDL_free(renderer);
    1.25 +        SDL_free(data);
    1.26          goto error;
    1.27      }
    1.28  
    1.29      if (GL_LoadFunctions(data) < 0) {
    1.30 -        GL_DestroyRenderer(renderer);
    1.31 +        SDL_GL_DeleteContext(data->context);
    1.32 +        SDL_free(renderer);
    1.33 +        SDL_free(data);
    1.34          goto error;
    1.35      }
    1.36  
     2.1 --- a/src/render/opengles2/SDL_render_gles2.c	Wed Oct 31 15:16:51 2018 -0700
     2.2 +++ b/src/render/opengles2/SDL_render_gles2.c	Wed Oct 31 20:17:53 2018 -0700
     2.3 @@ -2122,7 +2122,7 @@
     2.4  
     2.5      data = (GLES2_DriverContext *)SDL_calloc(1, sizeof(GLES2_DriverContext));
     2.6      if (!data) {
     2.7 -        GLES2_DestroyRenderer(renderer);
     2.8 +        SDL_free(renderer);
     2.9          SDL_OutOfMemory();
    2.10          goto error;
    2.11      }
    2.12 @@ -2134,16 +2134,21 @@
    2.13      /* Create an OpenGL ES 2.0 context */
    2.14      data->context = SDL_GL_CreateContext(window);
    2.15      if (!data->context) {
    2.16 -        GLES2_DestroyRenderer(renderer);
    2.17 +        SDL_free(renderer);
    2.18 +        SDL_free(data);
    2.19          goto error;
    2.20      }
    2.21      if (SDL_GL_MakeCurrent(window, data->context) < 0) {
    2.22 -        GLES2_DestroyRenderer(renderer);
    2.23 +        SDL_GL_DeleteContext(data->context);
    2.24 +        SDL_free(renderer);
    2.25 +        SDL_free(data);
    2.26          goto error;
    2.27      }
    2.28  
    2.29      if (GLES2_LoadFunctions(data) < 0) {
    2.30 -        GLES2_DestroyRenderer(renderer);
    2.31 +        SDL_GL_DeleteContext(data->context);
    2.32 +        SDL_free(renderer);
    2.33 +        SDL_free(data);
    2.34          goto error;
    2.35      }
    2.36