terminate zero-length char array with null char in stringUTFChars

This commit is contained in:
Joel Dice 2012-08-04 12:36:18 -06:00
parent ebd7f69c41
commit 248ff91d74

View File

@ -3667,33 +3667,31 @@ stringUTFChars(Thread* t, object string, unsigned start, unsigned length,
assert(t, static_cast<unsigned> assert(t, static_cast<unsigned>
(stringUTFLength(t, string, start, length)) == charsLength); (stringUTFLength(t, string, start, length)) == charsLength);
if (length) { object data = stringData(t, string);
object data = stringData(t, string); if (objectClass(t, data) == type(t, Machine::ByteArrayType)) {
if (objectClass(t, data) == type(t, Machine::ByteArrayType)) { memcpy(chars,
memcpy(chars, &byteArrayBody(t, data, stringOffset(t, string) + start),
&byteArrayBody(t, data, stringOffset(t, string) + start), length);
length); chars[length] = 0;
chars[length] = 0; } else {
} else { int j = 0;
int j = 0; for (unsigned i = 0; i < length; ++i) {
for (unsigned i = 0; i < length; ++i) { uint16_t c = charArrayBody
uint16_t c = charArrayBody (t, data, stringOffset(t, string) + start + i);
(t, data, stringOffset(t, string) + start + i); if(!c) { // null char
if(!c) { // null char chars[j++] = 0;
chars[j++] = 0; } else if (c < 0x80) { // ASCII char
} else if (c < 0x80) { // ASCII char chars[j++] = static_cast<char>(c);
chars[j++] = static_cast<char>(c); } else if (c < 0x800) { // two-byte char
} else if (c < 0x800) { // two-byte char chars[j++] = static_cast<char>(0x0c0 | (c >> 6));
chars[j++] = static_cast<char>(0x0c0 | (c >> 6)); chars[j++] = static_cast<char>(0x080 | (c & 0x03f));
chars[j++] = static_cast<char>(0x080 | (c & 0x03f)); } else { // three-byte char
} else { // three-byte char chars[j++] = static_cast<char>(0x0e0 | ((c >> 12) & 0x0f));
chars[j++] = static_cast<char>(0x0e0 | ((c >> 12) & 0x0f)); chars[j++] = static_cast<char>(0x080 | ((c >> 6) & 0x03f));
chars[j++] = static_cast<char>(0x080 | ((c >> 6) & 0x03f)); chars[j++] = static_cast<char>(0x080 | (c & 0x03f));
chars[j++] = static_cast<char>(0x080 | (c & 0x03f));
}
} }
chars[j] = 0;
} }
chars[j] = 0;
} }
} }