SDL: Copied X11 error handler code from SDL_x11opengl.c

From c5dd9964c1527c26dde57d88b5d9599094a01627 Mon Sep 17 00:00:00 2001
From: Fredrick Brennan <[EMAIL REDACTED]>
Date: Wed, 7 Apr 2021 12:14:16 -0400
Subject: [PATCH] Copied X11 error handler code from SDL_x11opengl.c

Avoids needing to malloc to hold the error string.
---
 src/video/x11/SDL_x11window.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/video/x11/SDL_x11window.c b/src/video/x11/SDL_x11window.c
index 70d7e3230..19bae22b8 100644
--- a/src/video/x11/SDL_x11window.c
+++ b/src/video/x11/SDL_x11window.c
@@ -734,9 +734,14 @@ X11_SetWindowTitle(_THIS, SDL_Window * window)
     status = X11_XChangeProperty(display, data->xwindow, _NET_WM_NAME, UTF8_STRING, 8, 0, (const unsigned char *) title, strlen(title));
 
     if (status != Success) {
-        char* x11err = malloc(1024);
-        X11_XGetErrorText(display, status, x11err, 1024);
-        SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Error when setting X11 window title to %s: %s\n", title, x11err);
+        char *x11_error = NULL;
+        char x11_error_locale[256];
+        if (X11_XGetErrorText(display, status, x11_error_locale, sizeof(x11_error_locale)) == Success)
+        {
+            x11_error = SDL_iconv_string("UTF-8", "", x11_error_locale, SDL_strlen(x11_error_locale)+1);
+            SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Error when setting X11 window title to %s: %s\n", title, x11_error);
+            SDL_free(x11_error);
+        }
     }
 
     X11_XFlush(display);