[PATCH] wglDeleteContext should fail on a garbage hglrc. When we receive garbage ctx->hdc is invalid which leads to a crash. Retrieve a default DC and use it to pass hglrc to the display driver. Luckily the opengl code maintains a list of valid opengl contexts, so it can detect whether the hglrc is valid or not. :) This fixes bug 7691.

Roderick Colenbrander thunderbird2k at gmx.net
Sat Feb 23 10:04:01 CST 2008


---
 dlls/gdi32/opengl.c          |   10 ++++++----
 dlls/opengl32/tests/opengl.c |   22 ++++++++++++++++++++++
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/dlls/gdi32/opengl.c b/dlls/gdi32/opengl.c
index f948cda..2bf9775 100644
--- a/dlls/gdi32/opengl.c
+++ b/dlls/gdi32/opengl.c
@@ -112,14 +112,16 @@ BOOL WINAPI wglDeleteContext(HGLRC hglrc)
 {
     DC *dc;
     BOOL ret = FALSE;
-    OPENGL_Context ctx = (OPENGL_Context)hglrc;
 
     TRACE("hglrc: (%p)\n", hglrc);
-    if(ctx == NULL)
+    if(hglrc == NULL)
+    {
+        SetLastError(ERROR_INVALID_HANDLE);
         return FALSE;
+    }
 
-    /* Retrieve the HDC associated with the context to access the display driver */
-    dc = get_dc_ptr(ctx->hdc);
+    /* Create a new device context instead of trying the hdc associated with the hglrc as the hglrc could be garbage */
+    dc = OPENGL_GetDefaultDC();
     if (!dc) return FALSE;
 
     if (!dc->funcs->pwglDeleteContext) FIXME(" :stub\n");
diff --git a/dlls/opengl32/tests/opengl.c b/dlls/opengl32/tests/opengl.c
index 456bf79..103a18f 100644
--- a/dlls/opengl32/tests/opengl.c
+++ b/dlls/opengl32/tests/opengl.c
@@ -262,6 +262,27 @@ static void test_colorbits(HDC hdc)
                                        iAttribRet[0], iAttribRet[1]);
 }
 
+static void test_deletecontext()
+{
+    HGLRC hglrc;
+    BOOL res;
+    DWORD error;
+    
+    /* Test what happens in case of wglDeleteContext(NULL) */
+    hglrc = NULL;
+    res = wglDeleteContext(hglrc);
+    ok(res == FALSE, "wglDeleteContext(NULL) should fail but it passed!");
+    error = GetLastError();
+    ok(error == ERROR_INVALID_HANDLE, "Expected from GetLastError(): ERROR_INVALID_HANDLE but recevied %x\n", error);
+
+    /* Test what happens in case of wglDeleteContext(garbage) */
+    hglrc = (HGLRC)0x12345678;
+    res = wglDeleteContext(hglrc);
+    ok(res == FALSE, "wglDeleteContext(0x1245678) should fail but it passed!");
+    error = GetLastError();
+    ok(error == ERROR_INVALID_HANDLE, "Expected from GetLastError(): ERROR_INVALID_HANDLE but recevied %x\n", error);
+}
+
 static void test_gdi_dbuf(HDC hdc)
 {
     const int iAttribList[] = { WGL_SUPPORT_GDI_ARB, WGL_DOUBLE_BUFFER_ARB };
@@ -392,6 +413,7 @@ START_TEST(opengl)
         init_functions();
 
         test_setpixelformat(hdc);
+        test_deletecontext();
         test_colorbits(hdc);
         test_gdi_dbuf(hdc);
 
-- 
1.5.3.8


--========GMX285271203787278214642--



More information about the wine-patches mailing list