[NSData initWithBase64EncodedString]: Fix decoding of an empty string

Don't call `NSZoneRealloc` with a length of 0, but free the zone and return an empty `NSData` buffer instead.
This commit is contained in:
Frederik Carlier 2023-04-06 20:11:35 +00:00
parent 1cb6ef8572
commit 081f890be8
2 changed files with 13 additions and 0 deletions

View file

@ -804,6 +804,13 @@ failure:
}
}
length = dst - result;
if (length == 0)
{
NSZoneFree(zone, result);
return [self initWithBytesNoCopy: 0 length: 0 freeWhenDone: YES];
}
if (options & NSDataBase64DecodingIgnoreUnknownCharacters)
{
/* If the decoded length is a lot shorter than expected (because we

View file

@ -127,6 +127,12 @@ int main()
PASS_EQUAL(data, ref, "base64 decoding Yml0bWFya2V0cyB1c2VyIGluZGVudGl0eQ==")
[data release];
data = [[NSData alloc] initWithBase64EncodedString: @"\n"
options: NSDataBase64DecodingIgnoreUnknownCharacters];
ref = [NSData dataWithBytes: "" length: 0];
PASS_EQUAL(data, ref, "base64 decoding empty string")
[data release];
[arp release]; arp = nil;
return 0;
}