Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -139,11 +139,16 @@ + (instancetype)stringWithUTF8String: (const char *)UTF8String length: (size_t)UTF8StringLength; /*! * @brief Creates a new OFString from a UTF-8 encoded C string without copying - * the string. + * the string, if possible. + * + * If initialization fails for whatever reason, the passed C string is free'd + * if `freeWhenDone` is true. + * + * @note OFMutableString always creates a copy! * * @param UTF8String A UTF-8 encoded C string to initialize the OFString with * @param freeWhenDone Whether to free the C string when the OFString gets * deallocated * @return A new autoreleased OFString @@ -380,13 +385,16 @@ - initWithUTF8String: (const char *)UTF8String length: (size_t)UTF8StringLength; /*! * @brief Initializes an already allocated OFString from an UTF-8 encoded C - * string without copying it, if possible. + * string without copying the string, if possible. * - * @note Mutable versions always create a copy! + * If initialization fails for whatever reason, the passed C string is free'd + * if `freeWhenDone` is true. + * + * @note OFMutableString always creates a copy! * * @param UTF8String A UTF-8 encoded C string to initialize the OFString with * @param freeWhenDone Whether to free the C string when it is not needed * anymore * @return An initialized OFString Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -848,11 +848,16 @@ } - initWithUTF8StringNoCopy: (char *)UTF8String freeWhenDone: (bool)freeWhenDone { - return [self initWithUTF8String: UTF8String]; + @try { + return [self initWithUTF8String: UTF8String]; + } @finally { + if (freeWhenDone) + free(UTF8String); + } } - initWithCString: (const char *)cString encoding: (of_string_encoding_t)encoding { Index: src/OFString_UTF8.m ================================================================== --- src/OFString_UTF8.m +++ src/OFString_UTF8.m @@ -391,11 +391,17 @@ } - initWithUTF8StringNoCopy: (char *)UTF8String freeWhenDone: (bool)freeWhenDone { - self = [super init]; + @try { + self = [super init]; + } @catch (id e) { + if (freeWhenDone) + free(UTF8String); + @throw e; + } @try { size_t UTF8StringLength = strlen(UTF8String); _s = &_storage;