Index: src/OFMutableString_UTF8.m ================================================================== --- src/OFMutableString_UTF8.m +++ src/OFMutableString_UTF8.m @@ -45,10 +45,25 @@ freeWhenDone: (bool)freeWhenDone { @try { self = [self initWithUTF8String: UTF8String]; } @finally { + if (freeWhenDone) + free(UTF8String); + } + + return self; +} + +- (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String + length: (size_t)UTF8StringLength + freeWhenDone: (bool)freeWhenDone +{ + @try { + self = [self initWithUTF8String: UTF8String + length: UTF8StringLength]; + } @finally { if (freeWhenDone) free(UTF8String); } return self; Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -335,10 +335,13 @@ length: (size_t)UTF8StringLength; /*! * @brief Creates a new OFString from a UTF-8 encoded C string without copying * 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 @@ -349,10 +352,13 @@ freeWhenDone: (bool)freeWhenDone; /*! * @brief Creates a new OFString from a UTF-8 encoded C string with the * specified length without copying 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 UTF8StringLength The length of the UTF-8 encoded C string @@ -594,10 +600,13 @@ length: (size_t)UTF8StringLength; /*! * @brief Initializes an already allocated OFString from an UTF-8 encoded C * string without copying 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 it is not needed @@ -609,10 +618,13 @@ /*! * @brief Initializes an already allocated OFString from an UTF-8 encoded C * string with the specified length without copying 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 UTF8StringLength The length of the UTF-8 encoded C string Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -860,19 +860,29 @@ } - (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String freeWhenDone: (bool)freeWhenDone { - return [self initWithUTF8String: UTF8String]; + @try { + return [self initWithUTF8String: UTF8String]; + } @finally { + if (freeWhenDone) + free(UTF8String); + } } - (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String length: (size_t)UTF8StringLength freeWhenDone: (bool)freeWhenDone { - return [self initWithUTF8String: UTF8String - length: UTF8StringLength]; + @try { + return [self initWithUTF8String: UTF8String + length: UTF8StringLength]; + } @finally { + if (freeWhenDone) + free(UTF8String); + } } - (instancetype)initWithCString: (const char *)cString encoding: (of_string_encoding_t)encoding { Index: src/OFString_UTF8.m ================================================================== --- src/OFString_UTF8.m +++ src/OFString_UTF8.m @@ -401,11 +401,17 @@ - (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String length: (size_t)UTF8StringLength freeWhenDone: (bool)freeWhenDone { - self = [super init]; + @try { + self = [super init]; + } @catch (id e) { + if (freeWhenDone) + free(UTF8String); + @throw e; + } @try { _s = &_storage; if (freeWhenDone)