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.
This commit is contained in:
Zvika Haramaty 2020-06-02 00:37:09 +03:00 committed by Eugene Sandulenko
parent ea7c11f532
commit 41f3d4b488

View file

@ -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;
}
}
iconv(iconvHandle, NULL, NULL, &dst, &outSize);
if (inSize == 0) {
// we're at the end - call one last time with NULLs
src = NULL;
}
}
// 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);