From b00142a846ceda985516a0bdec3c1d6a9f47bdee Mon Sep 17 00:00:00 2001 From: "teng.liu" Date: Sun, 8 Feb 2015 18:18:35 +0800 Subject: fallback solution for NSString decode error --- .../Source/SPMySQLResult Categories/Data Conversion.m | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'Frameworks') diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m b/Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m index 357eb53f..0ed8a51e 100644 --- a/Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m +++ b/Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m @@ -239,6 +239,17 @@ static inline NSString * _bitStringWithBytes(const char *bytes, NSUInteger lengt return returnString; } +static inline NSString * _convertStringDataSafely(const void *dataBytes, NSUInteger dataLength, NSStringEncoding aStringEncoding) +{ + NSString * result = [[[NSString alloc] initWithBytes:dataBytes length:dataLength encoding:aStringEncoding] autorelease]; + + if (result == nil) { + return [[[NSString alloc] initWithBytes:dataBytes length:dataLength encoding:NSASCIIStringEncoding] autorelease]; + } + + return result; +} + /** * Converts stored string data - which may contain nul bytes - to a native * Objective-C string, using the current class encoding. @@ -247,9 +258,9 @@ static inline NSString * _convertStringData(const void *dataBytes, NSUInteger da { // Fast case - if not using a preview length, or if the data length is shorter, return the requested data. - if (previewLength == NSNotFound || dataLength <= previewLength) { - return [[[NSString alloc] initWithBytes:dataBytes length:dataLength encoding:aStringEncoding] autorelease]; - } + if (previewLength == NSNotFound || dataLength <= previewLength) { + return _convertStringDataSafely(dataBytes, dataLength, aStringEncoding); + } NSUInteger i = 0, characterLength = 0, byteLength = previewLength; uint16_t continuationStart, continuationEnd; @@ -394,7 +405,7 @@ static inline NSString * _convertStringData(const void *dataBytes, NSUInteger da // If returning the full string, use a fast path if (byteLength >= dataLength) { - return [[[NSString alloc] initWithBytes:dataBytes length:dataLength encoding:aStringEncoding] autorelease]; + return _convertStringDataSafely(dataBytes, dataLength, aStringEncoding); } // Get a string using the calculated details -- cgit v1.2.3