Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 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 @@ -359,6 +359,10 @@ int32_t GlobalizationNative_GetSortKeyNative(const uint16_t* localeName, int32_t
return (int32_t)transformedStringBytes;
NSRange range = NSMakeRange(0, [transformedString length]);
NSUInteger usedLength = 0;
// 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;
BOOL result = [transformedString getBytes:sortKey maxLength:transformedStringBytes usedLength:&usedLength encoding:NSUTF16StringEncoding options:0 range:range remainingRange:NULL];
if (result)
return (int32_t)usedLength;
Expand Down