-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CoreFoundation's CFStringRef#stringValue doesn't add space for terminating null #1342
Comments
Reproduction also possible by altering existing test case to a string of all 3-byte utf8 characters. Adding 1 seems the simplest solution. Tried to implement |
So I thought this was fixed but had a suspicion it wasn't. UTF-8 takes up to 4 bytes, but the CoreFoundation function I think it's best to just multiply by 4 and add one. |
Here's the macos Source code with the incorrect calculation. https://github.com/opensource-apple/CF/blob/master/CFString.c#L465 |
CFStringRef#stringValue follows the following steps to create a String:
In the edge case where every character in the String is the max encoding length, there is no space for a null. To reproduce:
Output:
Proposed fix: Simply add 1 to the calculated max number of bytes
Possible improvement: Just multiply the length by 3 and add 1; or multiply the length by 4.
Other option: Map and call
CFStringGetBytes
passing null to getusedBufLen
which will include the space for the null.The text was updated successfully, but these errors were encountered: