From 68e9096762994b455b7465dae9e5d28aca4f60b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guilherme=20Ara=C3=BAjo?= Date: Fri, 20 Nov 2026 10:39:54 -0300 Subject: [PATCH 1/2] sqlite: bind empty ArrayBuffer as empty blob MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A zero-length ArrayBuffer or SharedArrayBuffer can have a null data pointer, and SQLite treats a null blob pointer as NULL. As a result, such values were bound as parameters, and returned from user-defined functions, as NULL instead of an empty blob. Use sqlite3_bind_zeroblob() and sqlite3_result_zeroblob() for empty buffers. Fixes: https://github.com/nodejs/node/issues/67188 Refs: https://github.com/nodejs/node/pull/62061 Assisted-by: Claude Opus 5.5 Signed-off-by: Guilherme Araújo --- src/node_sqlite.cc | 22 ++++++++---- test/parallel/test-sqlite-custom-functions.js | 24 +++++++++++++ .../test-sqlite-typed-array-and-data-view.js | 35 +++++++++++++++++++ 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 3b59007a1ce1..ba10d4e56c52 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -337,7 +337,12 @@ void JSValueToSQLiteResult(Isolate* isolate, } else if (value->IsArrayBufferView() || value->IsArrayBuffer() || value->IsSharedArrayBuffer()) { ArrayBufferViewContents buf(value); - sqlite3_result_blob64(ctx, buf.data(), buf.length(), SQLITE_TRANSIENT); + if (buf.length() == 0) { + // Empty buffers may lack a data pointer, which SQLite treats as NULL. + sqlite3_result_zeroblob(ctx, 0); + } else { + sqlite3_result_blob64(ctx, buf.data(), buf.length(), SQLITE_TRANSIENT); + } } else if (value->IsBigInt()) { bool lossless; int64_t as_int = value.As()->Int64Value(&lossless); @@ -3632,11 +3637,16 @@ bool Statement::BindValue(const Local& value, const int index) { } else if (value->IsArrayBufferView() || value->IsArrayBuffer() || value->IsSharedArrayBuffer()) { ArrayBufferViewContents buf(value); - r = sqlite3_bind_blob64(statement_.get(), - index, - buf.data(), - static_cast(buf.length()), - SQLITE_TRANSIENT); + if (buf.length() == 0) { + // Empty buffers may lack a data pointer, which SQLite treats as NULL. + r = sqlite3_bind_zeroblob(statement_.get(), index, 0); + } else { + r = sqlite3_bind_blob64(statement_.get(), + index, + buf.data(), + static_cast(buf.length()), + SQLITE_TRANSIENT); + } } else if (value->IsBoolean()) { r = sqlite3_bind_int(statement_.get(), index, value->IsTrue() ? 1 : 0); } else if (value->IsBigInt()) { diff --git a/test/parallel/test-sqlite-custom-functions.js b/test/parallel/test-sqlite-custom-functions.js index 0a4b80b80ae2..1dea1515f429 100644 --- a/test/parallel/test-sqlite-custom-functions.js +++ b/test/parallel/test-sqlite-custom-functions.js @@ -322,6 +322,30 @@ suite('Database.prototype.function()', () => { }); }); + test('empty buffers are returned as empty blobs, not NULL', () => { + const db = new Database(':memory:'); + db.function('retEmptyArrayBuffer', () => new ArrayBuffer(0)); + db.function('retEmptySharedArrayBuffer', () => new SharedArrayBuffer(0)); + db.function('retEmptyUint8Array', () => { + return new Uint8Array(new ArrayBuffer(0)); + }); + const stmt = db.prepare(`SELECT + typeof(retEmptyArrayBuffer()) AS arrayBufferType, + typeof(retEmptySharedArrayBuffer()) AS sharedArrayBufferType, + typeof(retEmptyUint8Array()) AS uint8ArrayType, + retEmptyArrayBuffer() AS arrayBuffer, + retEmptySharedArrayBuffer() AS sharedArrayBuffer + `); + assert.deepStrictEqual(stmt.get(), { + __proto__: null, + arrayBufferType: 'blob', + sharedArrayBufferType: 'blob', + uint8ArrayType: 'blob', + arrayBuffer: new Uint8Array(), + sharedArrayBuffer: new Uint8Array(), + }); + }); + test('throws if returned BigInt is too large for SQLite', () => { const db = new Database(':memory:'); db.function('retBigInt', () => { diff --git a/test/parallel/test-sqlite-typed-array-and-data-view.js b/test/parallel/test-sqlite-typed-array-and-data-view.js index e13b8b33daab..b95ecea1403b 100644 --- a/test/parallel/test-sqlite-typed-array-and-data-view.js +++ b/test/parallel/test-sqlite-typed-array-and-data-view.js @@ -120,3 +120,38 @@ suite('Statement with ArrayBuffer and SharedArrayBuffer', () => { }); } }); + +suite('Statement with empty buffers', () => { + const emptyBuffers = [ + ['ArrayBuffer', () => new ArrayBuffer(0)], + ['SharedArrayBuffer', () => new SharedArrayBuffer(0)], + ['Uint8Array', () => new Uint8Array()], + ['Uint8Array on empty ArrayBuffer', () => new Uint8Array(new ArrayBuffer(0))], + ['DataView on empty ArrayBuffer', () => new DataView(new ArrayBuffer(0))], + ]; + + for (const [displayName, createBuffer] of emptyBuffers) { + test(`${displayName} binds as an empty blob, not NULL`, (t) => { + const db = new Database(':memory:'); + t.after(() => { db.close(); }); + db.exec('CREATE TABLE test (data BLOB NOT NULL)'); + const buffer = createBuffer(); + // Anonymous binding + { + const stmt = db.prepare('SELECT typeof(?) AS type'); + t.assert.strictEqual(stmt.get(buffer).type, 'blob'); + } + // Named binding (object) + { + const stmt = db.prepare('SELECT typeof($data) AS type'); + t.assert.strictEqual(stmt.get({ '$data': buffer }).type, 'blob'); + } + // Insert into a NOT NULL column and read it back + { + db.prepare('INSERT INTO test VALUES (?)').run(buffer); + const row = db.prepare('SELECT data FROM test').get(); + t.assert.deepStrictEqual(row.data, new Uint8Array()); + } + }); + } +}); From a8da587cb7543fe0c6070ade33a2b9b0211b1cf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guilherme=20Ara=C3=BAjo?= Date: Fri, 20 Nov 2026 11:07:34 -0300 Subject: [PATCH 2/2] fixup! sqlite: bind empty ArrayBuffer as empty blob --- test/parallel/test-sqlite-custom-functions.js | 4 ++-- .../test-sqlite-typed-array-and-data-view.js | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/parallel/test-sqlite-custom-functions.js b/test/parallel/test-sqlite-custom-functions.js index 1dea1515f429..c6c5fa0388ce 100644 --- a/test/parallel/test-sqlite-custom-functions.js +++ b/test/parallel/test-sqlite-custom-functions.js @@ -323,13 +323,13 @@ suite('Database.prototype.function()', () => { }); test('empty buffers are returned as empty blobs, not NULL', () => { - const db = new Database(':memory:'); + using db = new Database(':memory:'); db.function('retEmptyArrayBuffer', () => new ArrayBuffer(0)); db.function('retEmptySharedArrayBuffer', () => new SharedArrayBuffer(0)); db.function('retEmptyUint8Array', () => { return new Uint8Array(new ArrayBuffer(0)); }); - const stmt = db.prepare(`SELECT + using stmt = db.prepare(`SELECT typeof(retEmptyArrayBuffer()) AS arrayBufferType, typeof(retEmptySharedArrayBuffer()) AS sharedArrayBufferType, typeof(retEmptyUint8Array()) AS uint8ArrayType, diff --git a/test/parallel/test-sqlite-typed-array-and-data-view.js b/test/parallel/test-sqlite-typed-array-and-data-view.js index b95ecea1403b..b0766aaa01c1 100644 --- a/test/parallel/test-sqlite-typed-array-and-data-view.js +++ b/test/parallel/test-sqlite-typed-array-and-data-view.js @@ -132,25 +132,25 @@ suite('Statement with empty buffers', () => { for (const [displayName, createBuffer] of emptyBuffers) { test(`${displayName} binds as an empty blob, not NULL`, (t) => { - const db = new Database(':memory:'); - t.after(() => { db.close(); }); + using db = new Database(':memory:'); db.exec('CREATE TABLE test (data BLOB NOT NULL)'); const buffer = createBuffer(); // Anonymous binding { - const stmt = db.prepare('SELECT typeof(?) AS type'); + using stmt = db.prepare('SELECT typeof(?) AS type'); t.assert.strictEqual(stmt.get(buffer).type, 'blob'); } // Named binding (object) { - const stmt = db.prepare('SELECT typeof($data) AS type'); + using stmt = db.prepare('SELECT typeof($data) AS type'); t.assert.strictEqual(stmt.get({ '$data': buffer }).type, 'blob'); } // Insert into a NOT NULL column and read it back { - db.prepare('INSERT INTO test VALUES (?)').run(buffer); - const row = db.prepare('SELECT data FROM test').get(); - t.assert.deepStrictEqual(row.data, new Uint8Array()); + using insert = db.prepare('INSERT INTO test VALUES (?)'); + insert.run(buffer); + using select = db.prepare('SELECT data FROM test'); + t.assert.deepStrictEqual(select.get().data, new Uint8Array()); } }); }