From be36a6a9c9825e460296431ac89b35a3bc48f33a Mon Sep 17 00:00:00 2001 From: Elliot Fiske Date: Mon, 29 Aug 2016 15:36:06 -0700 Subject: [PATCH 1/2] Fix a crash if value isn't UTF8 string --- src/fmdb/FMDatabase.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/fmdb/FMDatabase.m b/src/fmdb/FMDatabase.m index 3947241f..aed8869d 100644 --- a/src/fmdb/FMDatabase.m +++ b/src/fmdb/FMDatabase.m @@ -1210,6 +1210,9 @@ int FMDBExecuteBulkSQLCallback(void *theBlockAsVoid, int columns, char **values, for (NSInteger i = 0; i < columns; i++) { NSString *key = [NSString stringWithUTF8String:names[i]]; id value = values[i] ? [NSString stringWithUTF8String:values[i]] : [NSNull null]; + if (value == nil) { + value = [NSNull null]; + } [dictionary setObject:value forKey:key]; } From d4a4fd8c69e8d58f22babde312fe577bd9a382f8 Mon Sep 17 00:00:00 2001 From: Elliot Fiske Date: Mon, 29 Aug 2016 18:55:11 -0700 Subject: [PATCH 2/2] Fix UTF8 bug, with unit test --- Tests/FMDatabaseTests.m | 11 +++++++++++ src/fmdb/FMDatabase.m | 3 +++ 2 files changed, 14 insertions(+) diff --git a/Tests/FMDatabaseTests.m b/Tests/FMDatabaseTests.m index 9c423dce..4776ff11 100644 --- a/Tests/FMDatabaseTests.m +++ b/Tests/FMDatabaseTests.m @@ -493,6 +493,17 @@ - (void)testUTF8Strings XCTAssertFalse([self.db hadError], @"Shouldn't have any errors"); } +- (void)testNonUTF8Data +{ + [self.db executeUpdate:@"create table utf8bug (a blob)"]; + XCTAssertTrue(([self.db executeUpdate:@"insert into utf8bug (a) values (?)", [NSData dataWithBytes:"\xFF\xFF" length:4]])); + + [self.db executeStatements:@"select * from utf8bug" withResultBlock:^int(NSDictionary *results) { + NSLog(@"%@", results); + return 0; + }]; +} + - (void)testArgumentsInArray { [self.db executeUpdate:@"create table testOneHundredTwelvePointTwo (a text, b integer)"]; diff --git a/src/fmdb/FMDatabase.m b/src/fmdb/FMDatabase.m index 3947241f..a42665b9 100644 --- a/src/fmdb/FMDatabase.m +++ b/src/fmdb/FMDatabase.m @@ -1210,6 +1210,9 @@ int FMDBExecuteBulkSQLCallback(void *theBlockAsVoid, int columns, char **values, for (NSInteger i = 0; i < columns; i++) { NSString *key = [NSString stringWithUTF8String:names[i]]; id value = values[i] ? [NSString stringWithUTF8String:values[i]] : [NSNull null]; + if (value == nil) { + value = [NSString stringWithCString:values[i] encoding:NSASCIIStringEncoding]; + } [dictionary setObject:value forKey:key]; }