SDL: Fix potential memory leak in SDL_render_gles2.c (e3f47)

From e3f47809c4c4a9010abdcd43d1f8994565989038 Mon Sep 17 00:00:00 2001
From: Petar Popovic <[EMAIL REDACTED]>
Date: Fri, 1 Aug 2025 00:33:54 +0200
Subject: [PATCH] Fix potential memory leak in SDL_render_gles2.c

(cherry picked from commit 09221820f64502f65730413c4185d30eb82a4aa6)
---
 src/render/opengles2/SDL_render_gles2.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index d15f4f020eb77..410326b90eddf 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -1651,6 +1651,8 @@ static bool GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SD
         } else {
             renderdata->glGenTextures(1, &data->texture_v);
             if (!GL_CheckError("glGenTexures()", renderer)) {
+                SDL_free(data->pixel_data);
+                SDL_free(data);
                 return false;
             }
         }
@@ -1665,6 +1667,8 @@ static bool GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SD
         } else {
             renderdata->glGenTextures(1, &data->texture_u);
             if (!GL_CheckError("glGenTexures()", renderer)) {
+                SDL_free(data->pixel_data);
+                SDL_free(data);
                 return false;
             }
         }
@@ -1672,11 +1676,15 @@ static bool GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SD
         renderdata->glBindTexture(data->texture_type, data->texture_u);
         renderdata->glTexImage2D(data->texture_type, 0, format, (texture->w + 1) / 2, (texture->h + 1) / 2, 0, format, type, NULL);
         if (!GL_CheckError("glTexImage2D()", renderer)) {
+            SDL_free(data->pixel_data);
+            SDL_free(data);
             return false;
         }
         SDL_SetNumberProperty(SDL_GetTextureProperties(texture), SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_U_NUMBER, data->texture_u);
 
         if (!SDL_GetYCbCRtoRGBConversionMatrix(texture->colorspace, texture->w, texture->h, 8)) {
+            SDL_free(data->pixel_data);
+            SDL_free(data);
             return SDL_SetError("Unsupported YUV colorspace");
         }
     } else if (data->nv12) {
@@ -1686,6 +1694,8 @@ static bool GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SD
         } else {
             renderdata->glGenTextures(1, &data->texture_u);
             if (!GL_CheckError("glGenTexures()", renderer)) {
+                SDL_free(data->pixel_data);
+                SDL_free(data);
                 return false;
             }
         }
@@ -1693,11 +1703,15 @@ static bool GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SD
         renderdata->glBindTexture(data->texture_type, data->texture_u);
         renderdata->glTexImage2D(data->texture_type, 0, GL_LUMINANCE_ALPHA, (texture->w + 1) / 2, (texture->h + 1) / 2, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, NULL);
         if (!GL_CheckError("glTexImage2D()", renderer)) {
+            SDL_free(data->pixel_data);
+            SDL_free(data);
             return false;
         }
         SDL_SetNumberProperty(SDL_GetTextureProperties(texture), SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_UV_NUMBER, data->texture_u);
 
         if (!SDL_GetYCbCRtoRGBConversionMatrix(texture->colorspace, texture->w, texture->h, 8)) {
+            SDL_free(data->pixel_data);
+            SDL_free(data);
             return SDL_SetError("Unsupported YUV colorspace");
         }
     }
@@ -1709,6 +1723,8 @@ static bool GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SD
     } else {
         renderdata->glGenTextures(1, &data->texture);
         if (!GL_CheckError("glGenTexures()", renderer)) {
+            SDL_free(data->pixel_data);
+            SDL_free(data);
             return false;
         }
     }