Skip to content

Commit 7363c8e

Browse files
gengjiawenaduh95
authored andcommitted
sqlite: skip query trace while an exception is pending
Database::TraceCallback published sqlite.db.query from SQLite's profile callback without checking for a pending JavaScript exception. SQLite runs that callback from sqlite3_step() when a statement fails and from sqlite3_reset(), so a throwing SQL function or an out-of-range integer conversion called subscriber JS while an exception was already set. Debug builds abort in V8's Invoke, and release builds drop the original error or replace it with an empty ERR_SQLITE_ERROR. Return before publishing when the isolate has a pending exception or JavaScript cannot be entered. Assisted-by: Cursor Grok 4.7 Signed-off-by: gengjiawen <technicalcute@gmail.com> PR-URL: #67538 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b923833 commit 7363c8e

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎src/node_sqlite.cc‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,6 +3369,16 @@ int Database::TraceCallback(unsigned int type,
33693369
return 0;
33703370
}
33713371

3372+
// The profile callback also runs when a statement fails and when it is
3373+
// reset, both of which can happen while a JavaScript exception is already
3374+
// pending: a throwing SQL function, or a row value that cannot be converted.
3375+
// Publishing here would enter V8 with that exception still set. Debug
3376+
// builds then abort in Invoke, and release builds drop the user's error or
3377+
// replace it with an empty ERR_SQLITE_ERROR.
3378+
if (!env->can_call_into_js() || env->isolate()->HasPendingException()) {
3379+
return 0;
3380+
}
3381+
33723382
// Entered before building the payload below, because allocating it can
33733383
// trigger a garbage collection that SQLite is not prepared for.
33743384
CallbackDepthGuard guard(db);

‎test/parallel/test-sqlite-diagnostic-channel.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,38 @@ suite('sqlite.db.query diagnostics channel', () => {
234234
{ __proto__: null, x: 1 },
235235
]);
236236
});
237+
238+
it('does not replace an exception that is already pending', (t) => {
239+
using db = new Database(':memory:');
240+
let calls = 0;
241+
const handler = () => {
242+
calls++;
243+
};
244+
dc.subscribe('sqlite.db.query', handler);
245+
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
246+
247+
db.function('boom', () => {
248+
throw new Error('boom');
249+
});
250+
assert.throws(() => db.prepare('SELECT boom()').get(), {
251+
message: 'boom',
252+
});
253+
254+
db.exec('CREATE TABLE t (a INTEGER)');
255+
db.exec('INSERT INTO t VALUES (9223372036854775807)');
256+
assert.throws(() => db.prepare('SELECT a FROM t').get(), {
257+
code: 'ERR_OUT_OF_RANGE',
258+
});
259+
assert.throws(() => db.prepare('SELECT a FROM t').all(), {
260+
code: 'ERR_OUT_OF_RANGE',
261+
});
262+
263+
// A query that does not throw is still published.
264+
const before = calls;
265+
assert.deepStrictEqual(db.prepare('SELECT 1 AS x').get(), {
266+
__proto__: null,
267+
x: 1,
268+
});
269+
assert.ok(calls > before);
270+
});
237271
});

0 commit comments

Comments
 (0)