Skip to content

Commit ec97d64

Browse files
authored
sqlite: bind empty ArrayBuffer as empty blob
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: #67188 Refs: #62061 Assisted-by: Claude Opus 5.5 Signed-off-by: Guilherme Araújo <arauujogui@gmail.com> PR-URL: #67657 Reviewed-By: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent d3fdcc6 commit ec97d64

3 files changed

Lines changed: 75 additions & 6 deletions

File tree

‎src/node_sqlite.cc‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,12 @@ void JSValueToSQLiteResult(Isolate* isolate,
337337
} else if (value->IsArrayBufferView() || value->IsArrayBuffer() ||
338338
value->IsSharedArrayBuffer()) {
339339
ArrayBufferViewContents<uint8_t> buf(value);
340-
sqlite3_result_blob64(ctx, buf.data(), buf.length(), SQLITE_TRANSIENT);
340+
if (buf.length() == 0) {
341+
// Empty buffers may lack a data pointer, which SQLite treats as NULL.
342+
sqlite3_result_zeroblob(ctx, 0);
343+
} else {
344+
sqlite3_result_blob64(ctx, buf.data(), buf.length(), SQLITE_TRANSIENT);
345+
}
341346
} else if (value->IsBigInt()) {
342347
bool lossless;
343348
int64_t as_int = value.As<BigInt>()->Int64Value(&lossless);
@@ -3632,11 +3637,16 @@ bool Statement::BindValue(const Local<Value>& value, const int index) {
36323637
} else if (value->IsArrayBufferView() || value->IsArrayBuffer() ||
36333638
value->IsSharedArrayBuffer()) {
36343639
ArrayBufferViewContents<uint8_t> buf(value);
3635-
r = sqlite3_bind_blob64(statement_.get(),
3636-
index,
3637-
buf.data(),
3638-
static_cast<sqlite3_uint64>(buf.length()),
3639-
SQLITE_TRANSIENT);
3640+
if (buf.length() == 0) {
3641+
// Empty buffers may lack a data pointer, which SQLite treats as NULL.
3642+
r = sqlite3_bind_zeroblob(statement_.get(), index, 0);
3643+
} else {
3644+
r = sqlite3_bind_blob64(statement_.get(),
3645+
index,
3646+
buf.data(),
3647+
static_cast<sqlite3_uint64>(buf.length()),
3648+
SQLITE_TRANSIENT);
3649+
}
36403650
} else if (value->IsBoolean()) {
36413651
r = sqlite3_bind_int(statement_.get(), index, value->IsTrue() ? 1 : 0);
36423652
} else if (value->IsBigInt()) {

‎test/parallel/test-sqlite-custom-functions.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,30 @@ suite('Database.prototype.function()', () => {
322322
});
323323
});
324324

325+
test('empty buffers are returned as empty blobs, not NULL', () => {
326+
using db = new Database(':memory:');
327+
db.function('retEmptyArrayBuffer', () => new ArrayBuffer(0));
328+
db.function('retEmptySharedArrayBuffer', () => new SharedArrayBuffer(0));
329+
db.function('retEmptyUint8Array', () => {
330+
return new Uint8Array(new ArrayBuffer(0));
331+
});
332+
using stmt = db.prepare(`SELECT
333+
typeof(retEmptyArrayBuffer()) AS arrayBufferType,
334+
typeof(retEmptySharedArrayBuffer()) AS sharedArrayBufferType,
335+
typeof(retEmptyUint8Array()) AS uint8ArrayType,
336+
retEmptyArrayBuffer() AS arrayBuffer,
337+
retEmptySharedArrayBuffer() AS sharedArrayBuffer
338+
`);
339+
assert.deepStrictEqual(stmt.get(), {
340+
__proto__: null,
341+
arrayBufferType: 'blob',
342+
sharedArrayBufferType: 'blob',
343+
uint8ArrayType: 'blob',
344+
arrayBuffer: new Uint8Array(),
345+
sharedArrayBuffer: new Uint8Array(),
346+
});
347+
});
348+
325349
test('throws if returned BigInt is too large for SQLite', () => {
326350
const db = new Database(':memory:');
327351
db.function('retBigInt', () => {

‎test/parallel/test-sqlite-typed-array-and-data-view.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,38 @@ suite('Statement with ArrayBuffer and SharedArrayBuffer', () => {
120120
});
121121
}
122122
});
123+
124+
suite('Statement with empty buffers', () => {
125+
const emptyBuffers = [
126+
['ArrayBuffer', () => new ArrayBuffer(0)],
127+
['SharedArrayBuffer', () => new SharedArrayBuffer(0)],
128+
['Uint8Array', () => new Uint8Array()],
129+
['Uint8Array on empty ArrayBuffer', () => new Uint8Array(new ArrayBuffer(0))],
130+
['DataView on empty ArrayBuffer', () => new DataView(new ArrayBuffer(0))],
131+
];
132+
133+
for (const [displayName, createBuffer] of emptyBuffers) {
134+
test(`${displayName} binds as an empty blob, not NULL`, (t) => {
135+
using db = new Database(':memory:');
136+
db.exec('CREATE TABLE test (data BLOB NOT NULL)');
137+
const buffer = createBuffer();
138+
// Anonymous binding
139+
{
140+
using stmt = db.prepare('SELECT typeof(?) AS type');
141+
t.assert.strictEqual(stmt.get(buffer).type, 'blob');
142+
}
143+
// Named binding (object)
144+
{
145+
using stmt = db.prepare('SELECT typeof($data) AS type');
146+
t.assert.strictEqual(stmt.get({ '$data': buffer }).type, 'blob');
147+
}
148+
// Insert into a NOT NULL column and read it back
149+
{
150+
using insert = db.prepare('INSERT INTO test VALUES (?)');
151+
insert.run(buffer);
152+
using select = db.prepare('SELECT data FROM test');
153+
t.assert.deepStrictEqual(select.get().data, new Uint8Array());
154+
}
155+
});
156+
}
157+
});

0 commit comments

Comments
 (0)