From 5f5abb680523e1adedd3fca2a8e252db01fc1c52 Mon Sep 17 00:00:00 2001 From: Eddy Jansson Date: Tue, 28 Feb 2023 17:50:26 +0100 Subject: [PATCH] Always allocate zt in output of SDL_iconv_string() Before this, the function could not be used on buffers, as it would not account for the zero-termination unless it was included in the input. --- include/SDL3/SDL_stdinc.h | 2 +- src/stdlib/SDL_iconv.c | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/SDL3/SDL_stdinc.h b/include/SDL3/SDL_stdinc.h index da59fe5334..16fed038db 100644 --- a/include/SDL3/SDL_stdinc.h +++ b/include/SDL3/SDL_stdinc.h @@ -662,7 +662,7 @@ extern DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, const char **inbuf, size_t * outbytesleft); /** - * This function converts a string between encodings in one pass, returning a + * This function converts a buffer or string between encodings in one pass, returning a * string that must be freed with SDL_free() or NULL on error. * * \since This function is available since SDL 3.0.0. diff --git a/src/stdlib/SDL_iconv.c b/src/stdlib/SDL_iconv.c index b22994659d..709d877236 100644 --- a/src/stdlib/SDL_iconv.c +++ b/src/stdlib/SDL_iconv.c @@ -808,7 +808,7 @@ SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, } stringsize = inbytesleft > 4 ? inbytesleft : 4; - string = (char *)SDL_malloc(stringsize); + string = (char *)SDL_malloc(stringsize + 1); if (string == NULL) { SDL_iconv_close(cd); return NULL; @@ -825,7 +825,7 @@ SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, { char *oldstring = string; stringsize *= 2; - string = (char *)SDL_realloc(string, stringsize); + string = (char *)SDL_realloc(string, stringsize + 1); if (string == NULL) { SDL_free(oldstring); SDL_iconv_close(cd); @@ -851,6 +851,7 @@ SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, break; } } + *outbuf = '\0'; SDL_iconv_close(cd); return string;