Skip to content

Commit

Permalink
[iOS][HybridGlobalization] Handle small buffer case in sortkey (#106062)
Browse files Browse the repository at this point in the history
* handle small buffer case in sortkey
  • Loading branch information
mkhamoyan authored Aug 9, 2024
1 parent a524db6 commit 0f2ce10
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/native/libs/System.Globalization.Native/pal_collation.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 0f2ce10

Please sign in to comment.