mirek / YAML.framework

Proper YAML support for Objective-C. Based on recommended libyaml.

Home Page:http://github.com/mirek/YAML.framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect string length passed to yaml_document_add_scalar() in __YAMLSerializationAddObject()

TheGS opened this issue · comments

In __YAMLSerializationAddObject(), there is a call to yaml_document_add_scalar() with a pointer to the UTF8 form of the NSString, but the number of Unicode characters is used as the byte length of that UTF8 string. This will definitely be incorrect if the string has any characters that require more than 1 byte to represent in UTF8. In any case, since -[NSString UTF8String] returns a C string, it's sufficient to use a value of -1... code in yaml_document_add_scalar() will detect this and call strlen() on the C string to get the correct string length.

Absolutely

result = yaml_document_add_scalar(document, NULL, (yaml_char_t *)[string UTF8String], (int) [string length], YAML_PLAIN_SCALAR_STYLE);
is incorrect, we need to pass -1 as you said or [string lengthOfBytesUsingEncoding: NSUTF8StringEncoding] which may be bit faster.