From 0f2ce100fb386fec5eb784cf501ee95881a14e47 Mon Sep 17 00:00:00 2001 From: Meri Khamoyan <96171496+mkhamoyan@users.noreply.github.com> Date: Fri, 9 Aug 2024 10:47:16 +0200 Subject: [PATCH] [iOS][HybridGlobalization] Handle small buffer case in sortkey (#106062) * handle small buffer case in sortkey --- .../libs/System.Globalization.Native/pal_collation.m | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/native/libs/System.Globalization.Native/pal_collation.m b/src/native/libs/System.Globalization.Native/pal_collation.m index f18066d0812e78..ebe0db5c2c202a 100644 --- a/src/native/libs/System.Globalization.Native/pal_collation.m +++ b/src/native/libs/System.Globalization.Native/pal_collation.m @@ -328,7 +328,7 @@ int32_t GlobalizationNative_GetSortKeyNative(const uint16_t* localeName, int32_t @autoreleasepool { if (cwStrLength == 0) { - if (sortKey != NULL) + if (sortKey != NULL && cbSortKeyLength > 0) sortKey[0] = '\0'; return 1; } @@ -343,7 +343,7 @@ int32_t GlobalizationNative_GetSortKeyNative(const uint16_t* localeName, int32_t // If the string is empty after removing weightless characters, return 1 if(sourceStringCleaned.length == 0) { - if (sortKey != NULL) + if (sortKey != NULL && cbSortKeyLength > 0) sortKey[0] = '\0'; return 1; } @@ -357,13 +357,16 @@ int32_t GlobalizationNative_GetSortKeyNative(const uint16_t* localeName, int32_t NSUInteger transformedStringBytes = [transformedString lengthOfBytesUsingEncoding: NSUTF16StringEncoding]; if (sortKey == NULL) return (int32_t)transformedStringBytes; + + // If the buffer is too small, return the required buffer size + // and throw exception in managed code + if (cbSortKeyLength < (int32_t)transformedStringBytes) + return (int32_t)transformedStringBytes; NSRange range = NSMakeRange(0, [transformedString length]); NSUInteger usedLength = 0; BOOL result = [transformedString getBytes:sortKey maxLength:transformedStringBytes usedLength:&usedLength encoding:NSUTF16StringEncoding options:0 range:range remainingRange:NULL]; if (result) return (int32_t)usedLength; - - (void)cbSortKeyLength; // ignore unused parameter return 0; } }