mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
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: 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 <arauujogui@gmail.com> PR-URL: https://github.com/nodejs/node/pull/67657 Reviewed-By: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
This commit is contained in:
1 parent
7f6fae9285
commit
cd86a6b002
3 files changed
+75
-6
No files matched your search
+16
-6
@@ -337,7 +337,12 @@ void JSValueToSQLiteResult(Isolate* isolate,
|
||||
} else if (value->IsArrayBufferView() || value->IsArrayBuffer() ||
|
||||
value->IsSharedArrayBuffer()) {
|
||||
ArrayBufferViewContents<uint8_t> 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<BigInt>()->Int64Value(&lossless);
|
||||
@@ -3632,11 +3637,16 @@ bool Statement::BindValue(const Local<Value>& value, const int index) {
|
||||
} else if (value->IsArrayBufferView() || value->IsArrayBuffer() ||
|
||||
value->IsSharedArrayBuffer()) {
|
||||
ArrayBufferViewContents<uint8_t> buf(value);
|
||||
r = sqlite3_bind_blob64(statement_.get(),
|
||||
index,
|
||||
buf.data(),
|
||||
static_cast<sqlite3_uint64>(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<sqlite3_uint64>(buf.length()),
|
||||
SQLITE_TRANSIENT);
|
||||
}
|
||||
} else if (value->IsBoolean()) {
|
||||
r = sqlite3_bind_int(statement_.get(), index, value->IsTrue() ? 1 : 0);
|
||||
} else if (value->IsBigInt()) {
|
||||
|
||||
@@ -322,6 +322,30 @@ suite('Database.prototype.function()', () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('empty buffers are returned as empty blobs, not NULL', () => {
|
||||
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));
|
||||
});
|
||||
using 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', () => {
|
||||
|
||||
@@ -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) => {
|
||||
using db = new Database(':memory:');
|
||||
db.exec('CREATE TABLE test (data BLOB NOT NULL)');
|
||||
const buffer = createBuffer();
|
||||
// Anonymous binding
|
||||
{
|
||||
using stmt = db.prepare('SELECT typeof(?) AS type');
|
||||
t.assert.strictEqual(stmt.get(buffer).type, 'blob');
|
||||
}
|
||||
// Named binding (object)
|
||||
{
|
||||
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
|
||||
{
|
||||
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());
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user