OFString: Fix parsing LLONG_MIN

FossilOrigin-Name: 6f7cdf414a32b65fb7bb40f834156898e77bb6901af75dd02afbf4cbb3aa2233
This commit is contained in:
Jonathan Schleifer 2025-03-11 01:10:08 +00:00
parent 41d11f0f19
commit 0832f23368
2 changed files with 18 additions and 6 deletions

View file

@ -2385,7 +2385,7 @@ OF_SINGLETON_METHODS
void *pool = objc_autoreleasePoolPush(); void *pool = objc_autoreleasePoolPush();
const char *UTF8String = self.UTF8String; const char *UTF8String = self.UTF8String;
bool negative = false; bool negative = false;
long long value = 0; unsigned long long value = 0;
while (OFASCIIIsSpace(*UTF8String)) while (OFASCIIIsSpace(*UTF8String))
UTF8String++; UTF8String++;
@ -2437,18 +2437,26 @@ OF_SINGLETON_METHODS
if (c >= base) if (c >= base)
@throw [OFInvalidFormatException exception]; @throw [OFInvalidFormatException exception];
if (LLONG_MAX / base < value || LLONG_MAX - (value * base) < c) if (ULLONG_MAX / base < value ||
ULLONG_MAX - (value * base) < c)
@throw [OFOutOfRangeException exception]; @throw [OFOutOfRangeException exception];
value = (value * base) + c; value = (value * base) + c;
} }
if (negative)
value *= -1;
objc_autoreleasePoolPop(pool); objc_autoreleasePoolPop(pool);
return value; if (negative) {
if (value > -(unsigned long long)LLONG_MIN)
@throw [OFOutOfRangeException exception];
return (long long)-value;
} else {
if (value > (unsigned long long)LLONG_MAX)
@throw [OFOutOfRangeException exception];
return (long long)value;
}
} }
- (unsigned long long)unsignedLongLongValue - (unsigned long long)unsignedLongLongValue

View file

@ -749,6 +749,10 @@ static const char *range80ToFF =
OTAssertEqual([[self.stringClass stringWithString: OTAssertEqual([[self.stringClass stringWithString:
@"\t\t\r\n"] longLongValueWithBase: 8], 0); @"\t\t\r\n"] longLongValueWithBase: 8], 0);
OTAssertEqual([[self.stringClass stringWithString:
([OFString stringWithFormat: @"%lld", LLONG_MIN])] longLongValue],
LLONG_MIN);
} }
- (void)testLongLongValueThrowsOnInvalidFormat - (void)testLongLongValueThrowsOnInvalidFormat