SDL-1.2: SDL_iconv_string: simplify recomputation of outbuf and outbytesleft

From d9acf40b68863e09381488c576929f0c83485fb4 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Fri, 10 May 2024 20:21:10 +0300
Subject: [PATCH] SDL_iconv_string: simplify recomputation of outbuf and
 outbytesleft

Fixes a -Wuse-after-free warning from gcc-13:

./src/stdlib/SDL_iconv.c: In function 'SDL_iconv_string':
./src/stdlib/SDL_iconv.c:863:59: warning: pointer 'oldstring' may be used after 'realloc' [-Wuse-after-free]
  863 |                                 outbuf = string + (outbuf - oldstring);
      |                                                   ~~~~~~~~^~~~~~~~~~~~
In file included from ./src/stdlib/SDL_iconv.c:26:
./include/SDL_stdinc.h:196:25: note: call to 'realloc' here
  196 | #define SDL_realloc     realloc
./src/stdlib/SDL_iconv.c:857:42: note: in expansion of macro 'SDL_realloc'
  857 |                                 string = SDL_realloc(string, stringsize);
      |                                          ^~~~~~~~~~~

Backport from SDL3 commit 22056268168fa62bb66af62ef648b7030c9522d9
---
 src/stdlib/SDL_iconv.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/stdlib/SDL_iconv.c b/src/stdlib/SDL_iconv.c
index c257c91c..1f8b8523 100644
--- a/src/stdlib/SDL_iconv.c
+++ b/src/stdlib/SDL_iconv.c
@@ -852,6 +852,7 @@ char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inb
 		switch (retCode) {
 		    case SDL_ICONV_E2BIG:
 			{
+				const ptrdiff_t diff = (ptrdiff_t) (outbuf - string);
 				char *oldstring = string;
 				stringsize *= 2;
 				string = SDL_realloc(string, stringsize);
@@ -860,8 +861,8 @@ char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inb
 					SDL_iconv_close(cd);
 					return NULL;
 				}
-				outbuf = string + (outbuf - oldstring);
-				outbytesleft = stringsize - (outbuf - string);
+				outbuf = string + diff;
+				outbytesleft = stringsize - diff;
 				SDL_memset(outbuf, 0, 4);
 				continue;
 			}