From de0ce29d73e18b5d6a5d4756ee2ef69d20954ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 26 Sep 2026 11:27:14 +0200 Subject: [PATCH] Encode strings with a NUL character Inspired from #1880 by @nesevis --- GRDB/Core/DatabaseFunction.swift | 12 ++++-- GRDB/Core/DatabaseValue.swift | 2 +- .../StandardLibrary/StandardLibrary.swift | 42 +++++++++++++++++-- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/GRDB/Core/DatabaseFunction.swift b/GRDB/Core/DatabaseFunction.swift index 1744ae769..db1ebb4f5 100644 --- a/GRDB/Core/DatabaseFunction.swift +++ b/GRDB/Core/DatabaseFunction.swift @@ -105,8 +105,10 @@ public final class DatabaseFunction: Identifiable, Sendable { sqlite3_result_int64(context, int64) case .double(let double): sqlite3_result_double(context, double) - case .string(let string): - sqlite3_result_text(context, string, -1, SQLITE_TRANSIENT) + case .string(var string): + string.withSQLiteText { + sqlite3_result_text(context, $0, $1, SQLITE_TRANSIENT) + } case .blob(let data): data.withUnsafeBytes { sqlite3_result_blob(context, $0.baseAddress, CInt($0.count), SQLITE_TRANSIENT) @@ -447,8 +449,10 @@ public final class DatabaseFunction: Identifiable, Sendable { sqlite3_result_int64(sqliteContext, int64) case .double(let double): sqlite3_result_double(sqliteContext, double) - case .string(let string): - sqlite3_result_text(sqliteContext, string, -1, SQLITE_TRANSIENT) + case .string(var string): + string.withSQLiteText { + sqlite3_result_text(sqliteContext, $0, $1, SQLITE_TRANSIENT) + } case .blob(let data): data.withUnsafeBytes { sqlite3_result_blob(sqliteContext, $0.baseAddress, CInt($0.count), SQLITE_TRANSIENT) diff --git a/GRDB/Core/DatabaseValue.swift b/GRDB/Core/DatabaseValue.swift index 9373810cb..b335249e9 100644 --- a/GRDB/Core/DatabaseValue.swift +++ b/GRDB/Core/DatabaseValue.swift @@ -227,7 +227,7 @@ extension DatabaseValue: StatementBinding { let code = double.bind(to: sqliteStatement, at: index) try checkBindingSuccess(code: code, sqliteStatement: sqliteStatement) return try body() - case .string(let string): + case .string(var string): return try string.withBinding(to: sqliteStatement, at: index, do: body) case .blob(let data): return try data.withBinding(to: sqliteStatement, at: index, do: body) diff --git a/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift b/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift index e0f828853..65e8c13b2 100644 --- a/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift +++ b/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift @@ -624,23 +624,57 @@ extension String: DatabaseValueConvertible, StatementColumnConvertible { } public func bind(to sqliteStatement: SQLiteStatement, at index: CInt) -> CInt { - sqlite3_bind_text(sqliteStatement, index, self, -1, SQLITE_TRANSIENT) + var string = self + return string.withSQLiteText { + sqlite3_bind_text(sqliteStatement, index, $0, $1, SQLITE_TRANSIENT) + } } /// Calls the given closure after binding a statement argument. /// /// The binding is valid only during the execution of this method. /// + /// This method is mutating because the string is made contiguous + /// if necessary. + /// /// - parameter sqliteStatement: An SQLite statement. /// - parameter index: 1-based index to statement arguments. /// - parameter body: The closure to execute when argument is bound. - func withBinding(to sqliteStatement: SQLiteStatement, at index: CInt, do body: () throws -> T) throws -> T { - try withCString { - let code = sqlite3_bind_text(sqliteStatement, index, $0, -1, nil /* SQLITE_STATIC */) + mutating func withBinding( + to sqliteStatement: SQLiteStatement, + at index: CInt, + do body: () throws -> T + ) throws -> T { + try withSQLiteText { + let code = sqlite3_bind_text(sqliteStatement, index, $0, $1, nil /* SQLITE_STATIC */) try checkBindingSuccess(code: code, sqliteStatement: sqliteStatement) return try body() } } + + /// Runs body over the content of this string as an UTF8 string. This + /// method exists because it deals with the ASCII NUL (\0) characters, + /// unlike C strings. + /// + /// This method is mutating because the string is made contiguous + /// if necessary. + mutating func withSQLiteText( + _ body: (UnsafePointer, CInt) throws -> T + ) rethrows -> T { + try withUTF8 { utf8 in + guard let baseAddress = utf8.baseAddress else { + // Empty string: yield a non-nil pointer to the NUL string + // terminator, so that functions such as sqlite3_bind_text() + // unambiguously interpret it as the empty string. + return try withUnsafePointer(to: 0 as CChar) { + try body($0, 0) + } + } + return try baseAddress.withMemoryRebound(to: CChar.self, capacity: utf8.count) { + try body($0, CInt(utf8.count)) + } + } + } }