Skip to content

Commit 3a6bd9c

Browse files
cjihrigtargos
authored andcommitted
sqlite: handle thrown errors in result callback
This commit updates the aggregate function 'result' callback handling to not crash when an exception is thrown. Fixes: #58425 PR-URL: #58426 Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com> Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent d86ff60 commit 3a6bd9c

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/node_sqlite.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,10 @@ class CustomAggregate {
375375
result = Local<Value>::New(isolate, agg->value);
376376
}
377377

378-
JSValueToSQLiteResult(isolate, ctx, result);
378+
if (!result.IsEmpty()) {
379+
JSValueToSQLiteResult(isolate, ctx, result);
380+
}
381+
379382
if (is_final) {
380383
DestroyAggregateData(ctx);
381384
}

test/parallel/test-sqlite-aggregate-function.mjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,27 @@ describe('step', () => {
309309
});
310310

311311
describe('result', () => {
312+
test('throws if result throws an error', (t) => {
313+
const db = new DatabaseSync(':memory:');
314+
t.after(() => db.close());
315+
db.exec('CREATE TABLE data (value INTEGER)');
316+
db.exec('INSERT INTO data VALUES (1), (2), (3)');
317+
db.aggregate('sum_int', {
318+
start: 0,
319+
step: (acc, value) => {
320+
return acc + value;
321+
},
322+
result: () => {
323+
throw new Error('result error');
324+
},
325+
});
326+
t.assert.throws(() => {
327+
db.prepare('SELECT sum_int(value) as result FROM data').get();
328+
}, {
329+
message: 'result error'
330+
});
331+
});
332+
312333
test('executes once when options.inverse is not present', (t) => {
313334
const db = new DatabaseSync(':memory:');
314335
t.after(() => db.close());

0 commit comments

Comments
 (0)