From 8862dc49aa03691b5cbd614dde1a99420d555cbd Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Fri, 29 Jul 2016 21:27:47 -0700 Subject: [PATCH] Util: Fix realloc semantics in utf16to8 (again) --- src/util/string.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/util/string.c b/src/util/string.c index 08e4a4292..0157d4403 100644 --- a/src/util/string.c +++ b/src/util/string.c @@ -187,12 +187,11 @@ char* utf16to8(const uint16_t* utf16, size_t length) { memcpy(utf8, buffer, bytes); offset = utf8 + bytes; } else if (utf8Length >= utf8TotalBytes) { + ptrdiff_t o = offset - utf8; char* newUTF8 = realloc(utf8, utf8TotalBytes * 2); - offset = offset - utf8 + newUTF8; - if (newUTF8 != utf8) { - free(utf8); - } + offset = o + newUTF8; if (!newUTF8) { + free(utf8); return 0; } utf8 = newUTF8;