From b79642ef742dd7681f732343dcdb63970ad733d5 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Wed, 1 Dec 2021 23:10:12 +0100 Subject: [PATCH] LibGL: Support missing context in `glGetError` and `glGetIntegerv` In its current state, ScummVM seems to invoke these methods just after destroying the current GL context. According to the OpenGL spec: "Issuing GL commands when the program does not have a current context results in undefined behavior, up to and including program termination." Our old behavior was to deref a `nullptr`, which isn't that great. For now, protect these two methods. If other ports seem to misbehave as well, we can always expand the check to other methods. --- Userland/Libraries/LibGL/GLContext.cpp | 1 + Userland/Libraries/LibGL/GLContext.h | 10 ++++++++++ Userland/Libraries/LibGL/GLUtils.cpp | 2 ++ 3 files changed, 13 insertions(+) diff --git a/Userland/Libraries/LibGL/GLContext.cpp b/Userland/Libraries/LibGL/GLContext.cpp index ac71d8f5071..4c8f7dc9774 100644 --- a/Userland/Libraries/LibGL/GLContext.cpp +++ b/Userland/Libraries/LibGL/GLContext.cpp @@ -5,6 +5,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include "GLContext.h" #include "SoftwareGLContext.h" #include diff --git a/Userland/Libraries/LibGL/GLContext.h b/Userland/Libraries/LibGL/GLContext.h index 0bad6be594a..3faf9f5c93c 100644 --- a/Userland/Libraries/LibGL/GLContext.h +++ b/Userland/Libraries/LibGL/GLContext.h @@ -14,6 +14,16 @@ namespace GL { +#define VERIFY_CURRENT_CONTEXT() \ + if (!g_gl_context) { \ + return; \ + } + +#define VERIFY_CURRENT_CONTEXT_OR_VALUE(value) \ + if (!g_gl_context) { \ + return value; \ + } + class GLContext { public: virtual ~GLContext(); diff --git a/Userland/Libraries/LibGL/GLUtils.cpp b/Userland/Libraries/LibGL/GLUtils.cpp index b580730c727..253d2f4caec 100644 --- a/Userland/Libraries/LibGL/GLUtils.cpp +++ b/Userland/Libraries/LibGL/GLUtils.cpp @@ -67,6 +67,7 @@ void glViewport(GLint x, GLint y, GLsizei width, GLsizei height) GLenum glGetError() { + VERIFY_CURRENT_CONTEXT_OR_VALUE(GL_NONE); return g_gl_context->gl_get_error(); } @@ -117,6 +118,7 @@ void glGetFloatv(GLenum pname, GLfloat* params) void glGetIntegerv(GLenum pname, GLint* data) { + VERIFY_CURRENT_CONTEXT(); g_gl_context->gl_get_integerv(pname, data); }