From 9858de3249bc2b91b11313c817112b1ab8500dc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 26 Sep 2026 11:07:35 +0200 Subject: [PATCH] Decode strings with a NUL character Inspired from #1880 by @nesevis --- GRDB/Core/DatabaseValue.swift | 8 ++++++-- GRDB/Core/Support/Foundation/Decimal.swift | 5 ++--- GRDB/Core/Support/Foundation/UUID.swift | 2 +- GRDB/Core/Support/StandardLibrary/StandardLibrary.swift | 4 +++- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/GRDB/Core/DatabaseValue.swift b/GRDB/Core/DatabaseValue.swift index 7a36b3477..9373810cb 100644 --- a/GRDB/Core/DatabaseValue.swift +++ b/GRDB/Core/DatabaseValue.swift @@ -147,7 +147,10 @@ public struct DatabaseValue: Hashable { case SQLITE_FLOAT: storage = .double(sqlite3_value_double(sqliteValue)) case SQLITE_TEXT: - storage = .string(String(cString: sqlite3_value_text(sqliteValue)!)) + let text = sqlite3_value_text(sqliteValue) + let byteCount = Int(sqlite3_value_bytes(sqliteValue)) + let string = String(decoding: UnsafeBufferPointer(start: text, count: byteCount), as: UTF8.self) + storage = .string(string) case SQLITE_BLOB: if let bytes = sqlite3_value_blob(sqliteValue) { let count = Int(sqlite3_value_bytes(sqliteValue)) @@ -171,7 +174,8 @@ public struct DatabaseValue: Hashable { case SQLITE_FLOAT: storage = .double(sqlite3_column_double(sqliteStatement, index)) case SQLITE_TEXT: - storage = .string(String(cString: sqlite3_column_text(sqliteStatement, index))) + let string = String(sqliteStatement: sqliteStatement, index: index) + storage = .string(string) case SQLITE_BLOB: if let bytes = sqlite3_column_blob(sqliteStatement, index) { let count = Int(sqlite3_column_bytes(sqliteStatement, index)) diff --git a/GRDB/Core/Support/Foundation/Decimal.swift b/GRDB/Core/Support/Foundation/Decimal.swift index afcb65b74..1a82f32f6 100644 --- a/GRDB/Core/Support/Foundation/Decimal.swift +++ b/GRDB/Core/Support/Foundation/Decimal.swift @@ -56,9 +56,8 @@ extension Decimal: StatementColumnConvertible { case SQLITE_FLOAT: self.init(sqlite3_column_double(sqliteStatement, index)) case SQLITE_TEXT: - self.init( - string: String(cString: sqlite3_column_text(sqliteStatement, index)!), - locale: _posixLocale) + let string = String(sqliteStatement: sqliteStatement, index: index) + self.init(string: string, locale: _posixLocale) default: return nil } diff --git a/GRDB/Core/Support/Foundation/UUID.swift b/GRDB/Core/Support/Foundation/UUID.swift index bb4a34210..8d4055915 100644 --- a/GRDB/Core/Support/Foundation/UUID.swift +++ b/GRDB/Core/Support/Foundation/UUID.swift @@ -89,7 +89,7 @@ extension UUID: StatementColumnConvertible { public init?(sqliteStatement: SQLiteStatement, index: CInt) { switch sqlite3_column_type(sqliteStatement, index) { case SQLITE_TEXT: - let string = String(cString: sqlite3_column_text(sqliteStatement, index)!) + let string = String(sqliteStatement: sqliteStatement, index: index) guard let uuid = UUID(uuidString: string) else { return nil } diff --git a/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift b/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift index 0fe494d15..e0f828853 100644 --- a/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift +++ b/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift @@ -592,7 +592,9 @@ extension String: DatabaseValueConvertible, StatementColumnConvertible { /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. public init(sqliteStatement: SQLiteStatement, index: CInt) { - self = String(cString: sqlite3_column_text(sqliteStatement, index)!) + let text = sqlite3_column_text(sqliteStatement, index) + let byteCount = Int(sqlite3_column_bytes(sqliteStatement, index)) + self = String(decoding: UnsafeBufferPointer(start: text, count: byteCount), as: UTF8.self) } /// Returns a TEXT database value.