From 41f3d4b488069ecc153a7aa06f8f13735c45d23d Mon Sep 17 00:00:00 2001 From: Zvika Haramaty Date: Tue, 2 Jun 2020 00:37:09 +0300 Subject: [PATCH] COMMON: Encoding::convertIconv - check errors on last call to `iconv` There was a bug when trying to convert the Hebrew string "\e0\e1\e2" from WINDOWS-1255 to UTF-32LE, because there was a last call to `iconv` with `NULL`s, without error checking, and buffer needed to be extended for that last one call. Now this call is inserted to the main `iconv` loop, with error checking. --- common/encoding.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/common/encoding.cpp b/common/encoding.cpp index efd1ae0d951..80fe8230e01 100644 --- a/common/encoding.cpp +++ b/common/encoding.cpp @@ -225,7 +225,7 @@ char *Encoding::convertIconv(const char *to, const char *from, const char *strin char *dst = buffer; bool error = false; - while (inSize > 0) { + while (true) { if (iconv(iconvHandle, &src, &inSize, &dst, &outSize) == ((size_t)-1)) { // from SDL's implementation of SDL_iconv_string (slightly altered) if (errno == E2BIG) { @@ -244,9 +244,18 @@ char *Encoding::convertIconv(const char *to, const char *from, const char *strin error = true; break; } + } else { + // we've successfully finished, after the last call with NULLs + if (inSize == 0 && src == NULL) { + break; + } + } + if (inSize == 0) { + // we're at the end - call one last time with NULLs + src = NULL; } } - iconv(iconvHandle, NULL, NULL, &dst, &outSize); + // Add a zero character to the end. Hopefuly UTF32 uses the most bytes from // all possible encodings, so add 4 zero bytes. buffer = (char *)realloc(buffer, stringSize + 4);