From 76f83eaa46bc09c55780ed08d9681f21c491da28 Mon Sep 17 00:00:00 2001 From: Evan Hahn Date: Wed, 14 Sep 2022 11:49:56 -0500 Subject: [PATCH] Fix crash when trying to fetch -1 as a UInt from the database Here's a simplified version of the code we currently have: let sql = """ SELECT COUNT(*) - 1 FROM my_table WHERE id >= :current_id """ return try UInt.fetchOne(db, sql: sql) This causes GRDB to throw an error when the result of the query is `-1` because `-1` can't be converted to an unsigned integer. This caused a crash. I believe the `- 1` subtraction exists to filter out the current interaction, so I updated the SQL to effectively be: SELECT COUNT(*) FROM my_table WHERE id > :current_id This should prevent us from ever selecting `-1`, and therefore prevent this crash. --- .../src/Storage/Database/Records/InteractionFinder.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SignalServiceKit/src/Storage/Database/Records/InteractionFinder.swift b/SignalServiceKit/src/Storage/Database/Records/InteractionFinder.swift index a650577244..71dbbcc2cc 100644 --- a/SignalServiceKit/src/Storage/Database/Records/InteractionFinder.swift +++ b/SignalServiceKit/src/Storage/Database/Records/InteractionFinder.swift @@ -1293,10 +1293,10 @@ public class GRDBInteractionFinder: NSObject, InteractionFinderAdapter { } let distanceSQL = """ - SELECT count(*) - 1 + SELECT COUNT(*) FROM \(InteractionRecord.databaseTableName) WHERE \(interactionColumn: .threadUniqueId) = ? - AND \(interactionColumn: .id) >= ? + AND \(interactionColumn: .id) > ? \(Self.filterStoryRepliesClause(for: storyReplyQueryMode)) \(excludePlaceholders ? filterPlaceholdersClause : "") """