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. (cherry picked from commit 5f5abb680523e1adedd3fca2a8e252db01fc1c52)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h
index f46a728..182ed86 100644
--- a/include/SDL_stdinc.h
+++ b/include/SDL_stdinc.h
@@ -688,7 +688,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 2.0.0.
diff --git a/src/stdlib/SDL_iconv.c b/src/stdlib/SDL_iconv.c
index a42f263..9448afc 100644
--- a/src/stdlib/SDL_iconv.c
+++ b/src/stdlib/SDL_iconv.c
@@ -811,7 +811,7 @@ char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inb
}
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;
@@ -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);
+ string = (char *)SDL_realloc(string, stringsize + 1);
if (string == NULL) {
SDL_free(oldstring);
SDL_iconv_close(cd);
@@ -855,6 +855,7 @@ char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inb
break;
}
}
+ *outbuf = '\0';
SDL_iconv_close(cd);
return string;