Skip to content

Commit 0f2ce10

Browse files
authored
[iOS][HybridGlobalization] Handle small buffer case in sortkey (#106062)
* handle small buffer case in sortkey
1 parent a524db6 commit 0f2ce10

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/native/libs/System.Globalization.Native/pal_collation.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ int32_t GlobalizationNative_GetSortKeyNative(const uint16_t* localeName, int32_t
328328
@autoreleasepool {
329329
if (cwStrLength == 0)
330330
{
331-
if (sortKey != NULL)
331+
if (sortKey != NULL && cbSortKeyLength > 0)
332332
sortKey[0] = '\0';
333333
return 1;
334334
}
@@ -343,7 +343,7 @@ int32_t GlobalizationNative_GetSortKeyNative(const uint16_t* localeName, int32_t
343343
// If the string is empty after removing weightless characters, return 1
344344
if(sourceStringCleaned.length == 0)
345345
{
346-
if (sortKey != NULL)
346+
if (sortKey != NULL && cbSortKeyLength > 0)
347347
sortKey[0] = '\0';
348348
return 1;
349349
}
@@ -357,13 +357,16 @@ int32_t GlobalizationNative_GetSortKeyNative(const uint16_t* localeName, int32_t
357357
NSUInteger transformedStringBytes = [transformedString lengthOfBytesUsingEncoding: NSUTF16StringEncoding];
358358
if (sortKey == NULL)
359359
return (int32_t)transformedStringBytes;
360+
361+
// If the buffer is too small, return the required buffer size
362+
// and throw exception in managed code
363+
if (cbSortKeyLength < (int32_t)transformedStringBytes)
364+
return (int32_t)transformedStringBytes;
360365
NSRange range = NSMakeRange(0, [transformedString length]);
361366
NSUInteger usedLength = 0;
362367
BOOL result = [transformedString getBytes:sortKey maxLength:transformedStringBytes usedLength:&usedLength encoding:NSUTF16StringEncoding options:0 range:range remainingRange:NULL];
363368
if (result)
364369
return (int32_t)usedLength;
365-
366-
(void)cbSortKeyLength; // ignore unused parameter
367370
return 0;
368371
}
369372
}

0 commit comments

Comments
 (0)