runtime: improve integer overflow handling in bounds checks

This commit is contained in:
Svyatoslav Scherbina
2017-02-21 17:19:14 +07:00
committed by SvyatoslavScherbina
parent 9e2fe888e4
commit 72ff0c87cc
2 changed files with 13 additions and 12 deletions
+6 -4
View File
@@ -12,8 +12,9 @@ static inline void copyImpl(KConstRef thiz, KInt fromIndex,
KRef destination, KInt toIndex, KInt count) { KRef destination, KInt toIndex, KInt count) {
const ArrayHeader* array = thiz->array(); const ArrayHeader* array = thiz->array();
ArrayHeader* destinationArray = destination->array(); ArrayHeader* destinationArray = destination->array();
if (fromIndex < 0 || fromIndex + count > array->count_ || if (count < 0 ||
toIndex < 0 || toIndex + count > destinationArray->count_) { fromIndex < 0 || count > array->count_ - fromIndex ||
toIndex < 0 || count > destinationArray->count_ - toIndex) {
ThrowArrayIndexOutOfBoundsException(); ThrowArrayIndexOutOfBoundsException();
} }
@@ -73,8 +74,9 @@ void Kotlin_Array_copyImpl(KConstRef thiz, KInt fromIndex,
KRef destination, KInt toIndex, KInt count) { KRef destination, KInt toIndex, KInt count) {
const ArrayHeader* array = thiz->array(); const ArrayHeader* array = thiz->array();
ArrayHeader* destinationArray = destination->array(); ArrayHeader* destinationArray = destination->array();
if (fromIndex < 0 || fromIndex + count > array->count_ || if (count < 0 ||
toIndex < 0 || toIndex + count > destinationArray->count_) { fromIndex < 0 || count > array->count_ - fromIndex ||
toIndex < 0 || count > destinationArray->count_ - toIndex) {
ThrowArrayIndexOutOfBoundsException(); ThrowArrayIndexOutOfBoundsException();
} }
if (fromIndex >= toIndex) { if (fromIndex >= toIndex) {
+7 -8
View File
@@ -81,8 +81,7 @@ KInt Kotlin_String_getStringLength(KString thiz) {
OBJ_GETTER(Kotlin_String_fromUtf8Array, KConstRef thiz, KInt start, KInt size) { OBJ_GETTER(Kotlin_String_fromUtf8Array, KConstRef thiz, KInt start, KInt size) {
const ArrayHeader* array = thiz->array(); const ArrayHeader* array = thiz->array();
RuntimeAssert(array->type_info() == theByteArrayTypeInfo, "Must use a byte array"); RuntimeAssert(array->type_info() == theByteArrayTypeInfo, "Must use a byte array");
if (start < 0 || size < 0 || if (start < 0 || size < 0 || size > array->count_ - start) {
start + size > array->count_ || start + size < start) {
ThrowArrayIndexOutOfBoundsException(); ThrowArrayIndexOutOfBoundsException();
} }
if (size == 0) { if (size == 0) {
@@ -97,8 +96,7 @@ OBJ_GETTER(Kotlin_String_fromUtf8Array, KConstRef thiz, KInt start, KInt size) {
OBJ_GETTER(Kotlin_String_fromCharArray, KConstRef thiz, KInt start, KInt size) { OBJ_GETTER(Kotlin_String_fromCharArray, KConstRef thiz, KInt start, KInt size) {
const ArrayHeader* array = thiz->array(); const ArrayHeader* array = thiz->array();
RuntimeAssert(array->type_info() == theCharArrayTypeInfo, "Must use a char array"); RuntimeAssert(array->type_info() == theCharArrayTypeInfo, "Must use a char array");
if (start < 0 || size < 0 || if (start < 0 || size < 0 || size > array->count_ - start) {
start + size > array->count_ || start + size < start) {
ThrowArrayIndexOutOfBoundsException(); ThrowArrayIndexOutOfBoundsException();
} }
@@ -221,8 +219,9 @@ OBJ_GETTER(Kotlin_String_toLowerCase, KString thiz) {
KBoolean Kotlin_String_regionMatches(KString thiz, KInt thizOffset, KBoolean Kotlin_String_regionMatches(KString thiz, KInt thizOffset,
KString other, KInt otherOffset, KString other, KInt otherOffset,
KInt length, KBoolean ignoreCase) { KInt length, KBoolean ignoreCase) {
if (thizOffset < 0 || thizOffset + length > thiz->count_ || if (length < 0 ||
otherOffset < 0 || otherOffset + length > other->count_) { thizOffset < 0 || length > thiz->count_ - thizOffset ||
otherOffset < 0 || length > other->count_ - otherOffset) {
return false; return false;
} }
const KChar* thizRaw = CharArrayAddressOfElementAt(thiz, thizOffset); const KChar* thizRaw = CharArrayAddressOfElementAt(thiz, thizOffset);
@@ -332,7 +331,7 @@ KInt Kotlin_String_lastIndexOfChar(KString thiz, KChar ch, KInt fromIndex) {
// TODO: or code up Knuth-Moris-Pratt. // TODO: or code up Knuth-Moris-Pratt.
KInt Kotlin_String_indexOfString(KString thiz, KString other, KInt fromIndex) { KInt Kotlin_String_indexOfString(KString thiz, KString other, KInt fromIndex) {
if (fromIndex < 0 || fromIndex > thiz->count_ || if (fromIndex < 0 || fromIndex > thiz->count_ ||
fromIndex + other->count_ > thiz->count_) { other->count_ > thiz->count_ - fromIndex) {
return -1; return -1;
} }
KInt count = thiz->count_; KInt count = thiz->count_;
@@ -348,7 +347,7 @@ KInt Kotlin_String_indexOfString(KString thiz, KString other, KInt fromIndex) {
KInt Kotlin_String_lastIndexOfString(KString thiz, KString other, KInt fromIndex) { KInt Kotlin_String_lastIndexOfString(KString thiz, KString other, KInt fromIndex) {
if (fromIndex < 0 || fromIndex > thiz->count_ || thiz->count_ == 0 || if (fromIndex < 0 || fromIndex > thiz->count_ || thiz->count_ == 0 ||
fromIndex + other->count_ > thiz->count_) { other->count_ > thiz->count_ - fromIndex) {
return false; return false;
} }
KInt count = thiz->count_; KInt count = thiz->count_;