SDL: egl: Check for a NULL pointer in SDL_EGL_GetProcAddress.

From 44d7b8b91d6c22247188cc926716f54106cb91b9 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Tue, 15 Nov 2022 13:57:01 -0500
Subject: [PATCH] egl: Check for a NULL pointer in SDL_EGL_GetProcAddress.

This happens on kmsdrm if you try to GetProcAddress before creating
a window.

Fixes #5399.
---
 src/video/SDL_egl.c | 33 +++++++++++++++++----------------
 1 file changed, 17 insertions(+), 16 deletions(-)

diff --git a/src/video/SDL_egl.c b/src/video/SDL_egl.c
index 28d02b132c62..2438faf00a71 100644
--- a/src/video/SDL_egl.c
+++ b/src/video/SDL_egl.c
@@ -244,27 +244,28 @@ SDL_bool SDL_EGL_HasExtension(_THIS, SDL_EGL_ExtensionType type, const char *ext
 void *
 SDL_EGL_GetProcAddress(_THIS, const char *proc)
 {
-    const Uint32 eglver = (((Uint32) _this->egl_data->egl_version_major) << 16) | ((Uint32) _this->egl_data->egl_version_minor);
-    const SDL_bool is_egl_15_or_later = eglver >= ((((Uint32) 1) << 16) | 5);
     void *retval = NULL;
+    if (_this->egl_data != NULL) {
+        const Uint32 eglver = (((Uint32) _this->egl_data->egl_version_major) << 16) | ((Uint32) _this->egl_data->egl_version_minor);
+        const SDL_bool is_egl_15_or_later = eglver >= ((((Uint32) 1) << 16) | 5);
 
-    /* EGL 1.5 can use eglGetProcAddress() for any symbol. 1.4 and earlier can't use it for core entry points. */
-    if (!retval && is_egl_15_or_later && _this->egl_data->eglGetProcAddress) {
-        retval = _this->egl_data->eglGetProcAddress(proc);
-    }
+        /* EGL 1.5 can use eglGetProcAddress() for any symbol. 1.4 and earlier can't use it for core entry points. */
+        if (!retval && is_egl_15_or_later && _this->egl_data->eglGetProcAddress) {
+            retval = _this->egl_data->eglGetProcAddress(proc);
+        }
 
-    #if !defined(__EMSCRIPTEN__) && !defined(SDL_VIDEO_DRIVER_VITA)  /* LoadFunction isn't needed on Emscripten and will call dlsym(), causing other problems. */
-    /* Try SDL_LoadFunction() first for EGL <= 1.4, or as a fallback for >= 1.5. */
-    if (!retval) {
-        retval = SDL_LoadFunction(_this->egl_data->opengl_dll_handle, proc);
-    }
-    #endif
+        #if !defined(__EMSCRIPTEN__) && !defined(SDL_VIDEO_DRIVER_VITA)  /* LoadFunction isn't needed on Emscripten and will call dlsym(), causing other problems. */
+        /* Try SDL_LoadFunction() first for EGL <= 1.4, or as a fallback for >= 1.5. */
+        if (!retval) {
+            retval = SDL_LoadFunction(_this->egl_data->opengl_dll_handle, proc);
+        }
+        #endif
 
-    /* Try eglGetProcAddress if we're on <= 1.4 and still searching... */
-    if (!retval && !is_egl_15_or_later && _this->egl_data->eglGetProcAddress) {
-        retval = _this->egl_data->eglGetProcAddress(proc);
+        /* Try eglGetProcAddress if we're on <= 1.4 and still searching... */
+        if (!retval && !is_egl_15_or_later && _this->egl_data->eglGetProcAddress) {
+            retval = _this->egl_data->eglGetProcAddress(proc);
+        }
     }
-
     return retval;
 }