SDL_GL_DeleteContext would leave an invalid current_glctx.
authorJørgen P. Tjernø <jorgen@valvesoftware.com>
Mon, 22 Apr 2013 18:15:08 -0700
changeset 7091 ff07fad908f8
parent 7090 c4ac8be0065b
child 7092 201eb92bb31e
SDL_GL_DeleteContext would leave an invalid current_glctx. Calling SDL_GL_DeleteContext wouldn't update current_glctx, so you could end up with use-after-free and other goodies when you deleted a context.
src/render/opengl/SDL_render_gl.c
src/video/SDL_video.c
--- a/src/render/opengl/SDL_render_gl.c	Mon Apr 22 18:15:00 2013 -0700
+++ b/src/render/opengl/SDL_render_gl.c	Mon Apr 22 18:15:08 2013 -0700
@@ -1241,8 +1241,7 @@
                 GL_CheckError("", renderer);
                 SDL_free(data->framebuffers);
                 data->framebuffers = nextnode;
-            }            
-            /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
+            }
             SDL_GL_DeleteContext(data->context);
         }
         SDL_free(data);
--- a/src/video/SDL_video.c	Mon Apr 22 18:15:00 2013 -0700
+++ b/src/video/SDL_video.c	Mon Apr 22 18:15:08 2013 -0700
@@ -2690,13 +2690,14 @@
 {
     int retval;
 
-    CHECK_WINDOW_MAGIC(window, -1);
-
-    if (!(window->flags & SDL_WINDOW_OPENGL)) {
-        return SDL_SetError("The specified window isn't an OpenGL window");
-    }
     if (!ctx) {
         window = NULL;
+    } else {
+        CHECK_WINDOW_MAGIC(window, -1);
+
+        if (!(window->flags & SDL_WINDOW_OPENGL)) {
+            return SDL_SetError("The specified window isn't an OpenGL window");
+        }
     }
 
     if ((window == _this->current_glwin) && (ctx == _this->current_glctx)) {
@@ -2758,7 +2759,11 @@
     if (!_this || !context) {
         return;
     }
-    _this->GL_MakeCurrent(_this, NULL, NULL);
+
+    if (_this->current_glctx == context) {
+        SDL_GL_MakeCurrent(NULL, NULL);
+    }
+
     _this->GL_DeleteContext(_this, context);
 }