From a35a06f9f82f72a98ed320ec12199b9187006675 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Fri, 1 Feb 2019 09:05:56 +0300 Subject: [PATCH] Fix range checks for strings for optimized builds. (#2607) --- backend.native/tests/runtime/text/utf8.kt | 4 ++-- runtime/src/main/cpp/KString.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/backend.native/tests/runtime/text/utf8.kt b/backend.native/tests/runtime/text/utf8.kt index c36d203959b..0428f4a26b2 100644 --- a/backend.native/tests/runtime/text/utf8.kt +++ b/backend.native/tests/runtime/text/utf8.kt @@ -172,7 +172,7 @@ fun test16to8CustomBorders() { checkUtf16to8Replacing("Hello!", intArrayOf('e'.toInt(), 'l'.toInt()), 1, 2) checkUtf16to8Replacing("Hello!", intArrayOf('o'.toInt(), '!'.toInt()), 4, 2) checkUtf16to8Replacing("Hello!", intArrayOf(), 0, 0) - checkUtf16to8Replacing("Hello!", intArrayOf(), 10, 0) + checkUtf16to8Replacing("Hello!", intArrayOf(), 6, 0) checkUtf16to8Replacing("\uD800\uDC00\uD800\uDC00\uD800\uDC00\uD800\uDC00", intArrayOf(-16, -112, -128, -128, -16, -112, -128, -128), 0, 4) @@ -206,7 +206,7 @@ fun test16to8CustomBorders() { checkUtf16to8Throwing("Hello!", intArrayOf('e'.toInt(), 'l'.toInt()), 1, 2) checkUtf16to8Throwing("Hello!", intArrayOf('o'.toInt(), '!'.toInt()), 4, 2) checkUtf16to8Throwing("Hello!", intArrayOf(), 0, 0) - checkUtf16to8Throwing("Hello!", intArrayOf(), 10, 0) + checkUtf16to8Throwing("Hello!", intArrayOf(), 6, 0) checkUtf16to8Throwing("\uD800\uDC00\uD800\uDC00\uD800\uDC00\uD800\uDC00", intArrayOf(-16, -112, -128, -128, -16, -112, -128, -128), 0, 4) diff --git a/runtime/src/main/cpp/KString.cpp b/runtime/src/main/cpp/KString.cpp index f3814a747d2..20239307978 100644 --- a/runtime/src/main/cpp/KString.cpp +++ b/runtime/src/main/cpp/KString.cpp @@ -50,7 +50,7 @@ OBJ_GETTER(utf8ToUtf16Impl, const char* rawString, const char* end, uint32_t cha template OBJ_GETTER(utf16ToUtf8Impl, KString thiz, KInt start, KInt size) { RuntimeAssert(thiz->type_info() == theStringTypeInfo, "Must use String"); - if (start < 0 || size < 0 || size > thiz->count_ - start) { + if (start < 0 || size < 0 || (size > static_cast(thiz->count_) - start)) { ThrowArrayIndexOutOfBoundsException(); } const KChar* utf16 = CharArrayAddressOfElementAt(thiz, start); @@ -963,8 +963,8 @@ KBoolean Kotlin_String_regionMatches(KString thiz, KInt thizOffset, KString other, KInt otherOffset, KInt length, KBoolean ignoreCase) { if (length < 0 || - thizOffset < 0 || length > thiz->count_ - thizOffset || - otherOffset < 0 || length > other->count_ - otherOffset) { + thizOffset < 0 || length > static_cast(thiz->count_) - thizOffset || + otherOffset < 0 || length > static_cast(other->count_) - otherOffset) { return false; } const KChar* thizRaw = CharArrayAddressOfElementAt(thiz, thizOffset); @@ -1111,7 +1111,7 @@ KInt Kotlin_String_indexOfString(KString thiz, KString other, KInt fromIndex) { if (fromIndex >= thiz->count_) { return (other->count_ == 0) ? thiz->count_ : -1; } - if (other->count_ > thiz->count_ - fromIndex) { + if (other->count_ > static_cast(thiz->count_) - fromIndex) { return -1; } // An empty string can be always found.