From 52d63ba26c2abcfcabbf5c644eaf464a104de8e1 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 5 Jul 2023 01:22:28 -0700
Subject: [PATCH] Do a full UCS4 zero termination on iconv converted strings
We don't necessarily know the size of the output characters, so do a full 32-bit zero termination on the output string.
This fixes garbage at the end of Windows clipboard text
(cherry picked from commit ecbbac7c7292300cf998a2ff04db3c721203b0b7)
---
src/stdlib/SDL_iconv.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/src/stdlib/SDL_iconv.c b/src/stdlib/SDL_iconv.c
index 9448afcf5856..fd706dbcbb44 100644
--- a/src/stdlib/SDL_iconv.c
+++ b/src/stdlib/SDL_iconv.c
@@ -810,15 +810,15 @@ char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inb
return NULL;
}
- stringsize = inbytesleft > 4 ? inbytesleft : 4;
- string = (char *)SDL_malloc(stringsize + 1);
+ stringsize = inbytesleft;
+ string = (char *)SDL_malloc(stringsize + sizeof(Uint32));
if (string == NULL) {
SDL_iconv_close(cd);
return NULL;
}
outbuf = string;
outbytesleft = stringsize;
- SDL_memset(outbuf, 0, 4);
+ SDL_memset(outbuf, 0, sizeof(Uint32));
while (inbytesleft > 0) {
const size_t oldinbytesleft = inbytesleft;
@@ -828,7 +828,7 @@ char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inb
{
char *oldstring = string;
stringsize *= 2;
- string = (char *)SDL_realloc(string, stringsize + 1);
+ string = (char *)SDL_realloc(string, stringsize + sizeof(Uint32));
if (string == NULL) {
SDL_free(oldstring);
SDL_iconv_close(cd);
@@ -836,7 +836,7 @@ char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inb
}
outbuf = string + (outbuf - oldstring);
outbytesleft = stringsize - (outbuf - string);
- SDL_memset(outbuf, 0, 4);
+ SDL_memset(outbuf, 0, sizeof(Uint32));
continue;
}
case SDL_ICONV_EILSEQ:
@@ -855,7 +855,7 @@ char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inb
break;
}
}
- *outbuf = '\0';
+ SDL_memset(outbuf, 0, sizeof(Uint32));
SDL_iconv_close(cd);
return string;