Encode strings with a NUL character

Inspired from #1880 by @nesevis
This commit is contained in:
Gwendal Roué committed 2026-09-26 16:34:14 +02:00
1 parent 9858de3249
commit de0ce29d73
3 files changed
+47 -9

No files matched your search

+8 -4
View File
@@ -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)
+1 -1
View File
@@ -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)
@@ -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<T>(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<T>(
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<T>(
_ body: (UnsafePointer<CChar>, 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))
}
}
}
}