Skip to content

Commit

Permalink
Do a full UCS4 zero termination on iconv converted strings
Browse files Browse the repository at this point in the history
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 ecbbac7)
  • Loading branch information
slouken committed Jul 6, 2023
1 parent bad5431 commit 52d63ba
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/stdlib/SDL_iconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -828,15 +828,15 @@ 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);
return NULL;
}
outbuf = string + (outbuf - oldstring);
outbytesleft = stringsize - (outbuf - string);
SDL_memset(outbuf, 0, 4);
SDL_memset(outbuf, 0, sizeof(Uint32));
continue;
}
case SDL_ICONV_EILSEQ:
Expand All @@ -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;
Expand Down

0 comments on commit 52d63ba

Please sign in to comment.