Skip to content

Commit 91d84e9

Browse files
authored
Remove try! statement from FailableIterator
My team has recently experienced crashes of an app in production related to SQLite.swift. Upon further review, we discovered that these crashes are occurring due to this `try!` encountering a `throw` from `failableNext()`. This PR resolves the issue by returning nil if a throw is encountered. Here is the entire thread that crashes. Note that it occurs when using the Apollo-iOS framework, found (https://github.com/apollographql/apollo-ios)[here]: ``` Thread 7 name: Thread 7 Crashed: 0 libswiftCore.dylib 0x000000010117f968 0x100fa0000 + 1964392 1 libswiftCore.dylib 0x000000010117f968 0x100fa0000 + 1964392 2 libswiftCore.dylib 0x000000010102f994 0x100fa0000 + 588180 3 SQLite 0x0000000100d7e80c _T06SQLite16FailableIteratorPAAE4next7ElementQzSgyFAA9StatementC_Tg5 + 212 (Statement.swift:211) 4 SQLite 0x0000000100d674c8 _T06SQLite10ConnectionC7prepares11AnySequenceVyAA3RowVGAA9QueryType_pKFs0D8IteratorVyAHGycfU_AHSgycfU_TA + 76 (Query.swift:927) 5 SQLite 0x0000000100d67578 _T06SQLite3RowVSgIxo_ADIxr_TRTA + 48 (Query.swift:0) 6 libswiftCore.dylib 0x000000010114e9bc 0x100fa0000 + 1763772 7 libswiftCore.dylib 0x000000010114e9fc 0x100fa0000 + 1763836 8 libswiftCore.dylib 0x000000010114ead0 0x100fa0000 + 1764048 9 libswiftCore.dylib 0x000000010102a8c8 0x100fa0000 + 567496 10 libswiftCore.dylib 0x000000010114e16c 0x100fa0000 + 1761644 11 libswiftCore.dylib 0x000000010118fd78 0x100fa0000 + 2030968 12 libswiftCore.dylib 0x0000000100fa8774 0x100fa0000 + 34676 13 libswiftCore.dylib 0x000000010115f5bc 0x100fa0000 + 1832380 14 libswiftCore.dylib 0x000000010114f4fc 0x100fa0000 + 1766652 15 Apollo 0x0000000100a01c3c _T06Apollo21SQLiteNormalizedCacheC13selectRecords33_0382DE2BE0B0836588D3F2CA65C602E7LLSayAA6RecordVGSaySSG7forKeys_tKFTf4gn_n + 1284 (SQLiteNormalizedCache.swift:0) 16 Apollo 0x0000000100a02710 _T06Apollo21SQLiteNormalizedCacheC12mergeRecords33_0382DE2BE0B0836588D3F2CA65C602E7LLs3SetVySSGAA06RecordO0V7records_tKFTf4gn_n + 116 (SQLiteNormalizedCache.swift:0) 17 Apollo 0x0000000100a031c8 _T06Apollo21SQLiteNormalizedCacheC5mergeAA7PromiseCys3SetVySSGGAA06RecordG0V7records_tFTf4gn_n + 304 (SQLiteNormalizedCache.swift:0) 18 Apollo 0x00000001009ff290 _T06Apollo21SQLiteNormalizedCacheCAA0cD0A2aDP5mergeAA7PromiseCys3SetVySSGGAA06RecordG0V7records_tFTW + 44 (SQLiteNormalizedCache.swift:0) 19 Apollo 0x000000010099d1ec _T06Apollo0A5StoreC7publishAA7PromiseCyytGAA9RecordSetV7records_SvSg7contexttFyyyc_ys5Error_pctcfU_yycfU_Tf4ggng_n + 164 (ApolloStore.swift:52) 20 Apollo 0x000000010099f788 _T06Apollo0A5StoreC7publishAA7PromiseCyytGAA9RecordSetV7records_SvSg7contexttFyyyc_ys5Error_pctcfU_yycfU_TA + 104 (ApolloStore.swift:0) 21 Apollo 0x00000001009c0f1c _T0Ix_IyB_TR + 36 (GraphQLQueryWatcher.swift:0) 22 libdispatch.dylib 0x00000001812b2a54 _dispatch_call_block_and_release + 24 (init.c:994) 23 libdispatch.dylib 0x00000001812b2a14 _dispatch_client_callout + 16 (object.m:502) 24 libdispatch.dylib 0x00000001812c1540 _dispatch_queue_concurrent_drain + 540 (inline_internal.h:2500) 25 libdispatch.dylib 0x00000001812bd304 _dispatch_queue_invoke$VARIANT$mp + 348 (queue.c:5304) 26 libdispatch.dylib 0x00000001812bfcf4 _dispatch_root_queue_drain + 600 (inline_internal.h:2539) 27 libdispatch.dylib 0x00000001812bfa38 _dispatch_worker_thread3 + 120 (queue.c:6104) 28 libsystem_pthread.dylib 0x000000018155b06c _pthread_wqthread + 1268 (pthread.c:2286) 29 libsystem_pthread.dylib 0x000000018155ab6c start_wqthread + 4 ```
1 parent c052d41 commit 91d84e9

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Sources/SQLite/Core/Statement.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,10 @@ public protocol FailableIterator : IteratorProtocol {
208208

209209
extension FailableIterator {
210210
public func next() -> Element? {
211-
return try! failableNext()
211+
if let next = try? failableNext() {
212+
return next
213+
}
214+
return nil
212215
}
213216
}
214217

0 commit comments

Comments
 (0)