From 248ff91d747f455ad5095651cf08b9a7d056142a Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Sat, 4 Aug 2012 12:36:18 -0600 Subject: [PATCH] terminate zero-length char array with null char in stringUTFChars --- src/machine.cpp | 52 ++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/src/machine.cpp b/src/machine.cpp index 964bfd5452..dbf2e12dfc 100644 --- a/src/machine.cpp +++ b/src/machine.cpp @@ -3667,34 +3667,32 @@ stringUTFChars(Thread* t, object string, unsigned start, unsigned length, assert(t, static_cast (stringUTFLength(t, string, start, length)) == charsLength); - if (length) { - object data = stringData(t, string); - if (objectClass(t, data) == type(t, Machine::ByteArrayType)) { - memcpy(chars, - &byteArrayBody(t, data, stringOffset(t, string) + start), - length); - chars[length] = 0; - } else { - int j = 0; - for (unsigned i = 0; i < length; ++i) { - uint16_t c = charArrayBody - (t, data, stringOffset(t, string) + start + i); - if(!c) { // null char - chars[j++] = 0; - } else if (c < 0x80) { // ASCII char - chars[j++] = static_cast(c); - } else if (c < 0x800) { // two-byte char - chars[j++] = static_cast(0x0c0 | (c >> 6)); - chars[j++] = static_cast(0x080 | (c & 0x03f)); - } else { // three-byte char - chars[j++] = static_cast(0x0e0 | ((c >> 12) & 0x0f)); - chars[j++] = static_cast(0x080 | ((c >> 6) & 0x03f)); - chars[j++] = static_cast(0x080 | (c & 0x03f)); - } + object data = stringData(t, string); + if (objectClass(t, data) == type(t, Machine::ByteArrayType)) { + memcpy(chars, + &byteArrayBody(t, data, stringOffset(t, string) + start), + length); + chars[length] = 0; + } else { + int j = 0; + for (unsigned i = 0; i < length; ++i) { + uint16_t c = charArrayBody + (t, data, stringOffset(t, string) + start + i); + if(!c) { // null char + chars[j++] = 0; + } else if (c < 0x80) { // ASCII char + chars[j++] = static_cast(c); + } else if (c < 0x800) { // two-byte char + chars[j++] = static_cast(0x0c0 | (c >> 6)); + chars[j++] = static_cast(0x080 | (c & 0x03f)); + } else { // three-byte char + chars[j++] = static_cast(0x0e0 | ((c >> 12) & 0x0f)); + chars[j++] = static_cast(0x080 | ((c >> 6) & 0x03f)); + chars[j++] = static_cast(0x080 | (c & 0x03f)); } - chars[j] = 0; - } - } + } + chars[j] = 0; + } } uint64_t