-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
sqlite: reject reentry into a running statement #65106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TrevorBurnham
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
TrevorBurnham:sqlite/guard-statement-reentry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+256
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,210 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| 'use strict'; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const { skipIfSQLiteMissing, mustCall } = require('../common'); | ||||||||||||||||||||||||||||||||||||||||||||
| skipIfSQLiteMissing(); | ||||||||||||||||||||||||||||||||||||||||||||
| const assert = require('node:assert'); | ||||||||||||||||||||||||||||||||||||||||||||
| const { suite, test } = require('node:test'); | ||||||||||||||||||||||||||||||||||||||||||||
| const { DatabaseSync } = require('node:sqlite'); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const reentryError = { | ||||||||||||||||||||||||||||||||||||||||||||
| code: 'ERR_INVALID_STATE', | ||||||||||||||||||||||||||||||||||||||||||||
| message: 'statement is currently being executed', | ||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| function newDbWithRows() { | ||||||||||||||||||||||||||||||||||||||||||||
| const db = new DatabaseSync(':memory:'); | ||||||||||||||||||||||||||||||||||||||||||||
| db.exec(` | ||||||||||||||||||||||||||||||||||||||||||||
| CREATE TABLE data (value INTEGER); | ||||||||||||||||||||||||||||||||||||||||||||
| INSERT INTO data VALUES (1), (2), (3); | ||||||||||||||||||||||||||||||||||||||||||||
| `); | ||||||||||||||||||||||||||||||||||||||||||||
| return db; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| suite('reentry into the running statement is rejected', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| for (const method of ['all', 'get', 'run']) { | ||||||||||||||||||||||||||||||||||||||||||||
| test(`statement.${method}() from its own UDF`, () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const db = newDbWithRows(); | ||||||||||||||||||||||||||||||||||||||||||||
| let statement; | ||||||||||||||||||||||||||||||||||||||||||||
| db.function('reenter', mustCall((value) => { | ||||||||||||||||||||||||||||||||||||||||||||
| assert.throws(() => statement[method](), reentryError); | ||||||||||||||||||||||||||||||||||||||||||||
| return value; | ||||||||||||||||||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| statement = db.prepare('SELECT reenter(value) AS value FROM data LIMIT 1'); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.deepStrictEqual(statement.get(), { __proto__: null, value: 1 }); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.strictEqual(db.isOpen, true); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add coverage for the binding-time reentry path as well.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
| test('iterator next() from its own UDF', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const db = newDbWithRows(); | ||||||||||||||||||||||||||||||||||||||||||||
| let iterator; | ||||||||||||||||||||||||||||||||||||||||||||
| db.function('reenter', mustCall((value) => { | ||||||||||||||||||||||||||||||||||||||||||||
| assert.throws(() => iterator.next(), reentryError); | ||||||||||||||||||||||||||||||||||||||||||||
| return value; | ||||||||||||||||||||||||||||||||||||||||||||
| }, 3)); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| iterator = db.prepare('SELECT reenter(value) AS value FROM data').iterate(); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.deepStrictEqual([...iterator].map((row) => row.value), [1, 2, 3]); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.strictEqual(db.isOpen, true); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| test('iterator return() from its own UDF', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const db = newDbWithRows(); | ||||||||||||||||||||||||||||||||||||||||||||
| let iterator; | ||||||||||||||||||||||||||||||||||||||||||||
| db.function('reenter', mustCall((value) => { | ||||||||||||||||||||||||||||||||||||||||||||
| assert.throws(() => iterator.return(), reentryError); | ||||||||||||||||||||||||||||||||||||||||||||
| return value; | ||||||||||||||||||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| iterator = db.prepare('SELECT reenter(value) AS value FROM data').iterate(); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.strictEqual(iterator.next().done, false); | ||||||||||||||||||||||||||||||||||||||||||||
| iterator.return(); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.strictEqual(db.isOpen, true); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| test('recursive get() reports the reentry rather than overflowing the stack', | ||||||||||||||||||||||||||||||||||||||||||||
| () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const db = new DatabaseSync(':memory:'); | ||||||||||||||||||||||||||||||||||||||||||||
| let statement; | ||||||||||||||||||||||||||||||||||||||||||||
| db.function('reenter', mustCall(() => { | ||||||||||||||||||||||||||||||||||||||||||||
| assert.throws(() => statement.get(), reentryError); | ||||||||||||||||||||||||||||||||||||||||||||
| return 1; | ||||||||||||||||||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| statement = db.prepare('SELECT reenter() AS value'); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.deepStrictEqual(statement.get(), { __proto__: null, value: 1 }); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| test('statement.close() from its own UDF', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const db = newDbWithRows(); | ||||||||||||||||||||||||||||||||||||||||||||
| let statement; | ||||||||||||||||||||||||||||||||||||||||||||
| db.function('reenter', mustCall((value) => { | ||||||||||||||||||||||||||||||||||||||||||||
| assert.throws(() => statement.close(), reentryError); | ||||||||||||||||||||||||||||||||||||||||||||
| return value; | ||||||||||||||||||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| statement = db.prepare('SELECT reenter(value) AS value FROM data LIMIT 1'); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.deepStrictEqual(statement.get(), { __proto__: null, value: 1 }); | ||||||||||||||||||||||||||||||||||||||||||||
| statement.close(); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| test('statement[Symbol.dispose]() from its own UDF', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const db = newDbWithRows(); | ||||||||||||||||||||||||||||||||||||||||||||
| let statement; | ||||||||||||||||||||||||||||||||||||||||||||
| db.function('reenter', mustCall((value) => { | ||||||||||||||||||||||||||||||||||||||||||||
| assert.throws(() => statement[Symbol.dispose](), reentryError); | ||||||||||||||||||||||||||||||||||||||||||||
| return value; | ||||||||||||||||||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| statement = db.prepare('SELECT reenter(value) AS value FROM data LIMIT 1'); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.deepStrictEqual(statement.get(), { __proto__: null, value: 1 }); | ||||||||||||||||||||||||||||||||||||||||||||
| statement[Symbol.dispose](); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| test('statement is usable again after the callback returns', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const db = newDbWithRows(); | ||||||||||||||||||||||||||||||||||||||||||||
| let statement; | ||||||||||||||||||||||||||||||||||||||||||||
| db.function('reenter', mustCall((value) => { | ||||||||||||||||||||||||||||||||||||||||||||
| assert.throws(() => statement.all(), reentryError); | ||||||||||||||||||||||||||||||||||||||||||||
| return value; | ||||||||||||||||||||||||||||||||||||||||||||
| }, 2)); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| statement = db.prepare('SELECT reenter(value) AS value FROM data LIMIT 1'); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.deepStrictEqual(statement.all(), [{ __proto__: null, value: 1 }]); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.deepStrictEqual(statement.all(), [{ __proto__: null, value: 1 }]); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| suite('a different statement remains usable from a callback', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| test('the lookup pattern still works', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const db = newDbWithRows(); | ||||||||||||||||||||||||||||||||||||||||||||
| db.exec('CREATE TABLE names (value INTEGER, name TEXT);' + | ||||||||||||||||||||||||||||||||||||||||||||
| "INSERT INTO names VALUES (1, 'one'), (2, 'two'), (3, 'three');"); | ||||||||||||||||||||||||||||||||||||||||||||
| const lookup = db.prepare('SELECT name FROM names WHERE value = ?'); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| db.function('name_of', mustCall((value) => lookup.get(value).name, 3)); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| assert.deepStrictEqual( | ||||||||||||||||||||||||||||||||||||||||||||
| db.prepare('SELECT name_of(value) AS name FROM data').all(), | ||||||||||||||||||||||||||||||||||||||||||||
| [ | ||||||||||||||||||||||||||||||||||||||||||||
| { __proto__: null, name: 'one' }, | ||||||||||||||||||||||||||||||||||||||||||||
| { __proto__: null, name: 'two' }, | ||||||||||||||||||||||||||||||||||||||||||||
| { __proto__: null, name: 'three' }, | ||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| test('a nested iterator over a different statement still works', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const db = newDbWithRows(); | ||||||||||||||||||||||||||||||||||||||||||||
| const inner = db.prepare('SELECT value FROM data'); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| db.function('sum_all', mustCall(() => { | ||||||||||||||||||||||||||||||||||||||||||||
| let total = 0; | ||||||||||||||||||||||||||||||||||||||||||||
| for (const row of inner.iterate()) { | ||||||||||||||||||||||||||||||||||||||||||||
| total += row.value; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| return total; | ||||||||||||||||||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| assert.deepStrictEqual( | ||||||||||||||||||||||||||||||||||||||||||||
| db.prepare('SELECT sum_all() AS total LIMIT 1').get(), | ||||||||||||||||||||||||||||||||||||||||||||
| { __proto__: null, total: 6 }, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| suite('SQL tag store reentry is rejected', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| for (const method of ['all', 'get', 'run']) { | ||||||||||||||||||||||||||||||||||||||||||||
| test(`tag store ${method} re-executing the same tag`, () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const db = newDbWithRows(); | ||||||||||||||||||||||||||||||||||||||||||||
| const sql = db.createTagStore(4); | ||||||||||||||||||||||||||||||||||||||||||||
| db.function('reenter', mustCall((value) => { | ||||||||||||||||||||||||||||||||||||||||||||
| assert.throws( | ||||||||||||||||||||||||||||||||||||||||||||
| () => sql[method]`SELECT reenter(value) AS value FROM data LIMIT 1`, | ||||||||||||||||||||||||||||||||||||||||||||
| reentryError, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| return value; | ||||||||||||||||||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| assert.deepStrictEqual( | ||||||||||||||||||||||||||||||||||||||||||||
| sql.get`SELECT reenter(value) AS value FROM data LIMIT 1`, | ||||||||||||||||||||||||||||||||||||||||||||
| { __proto__: null, value: 1 }, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.strictEqual(db.isOpen, true); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| suite('aggregate functions', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| test('reentry from an aggregate step is rejected', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const db = newDbWithRows(); | ||||||||||||||||||||||||||||||||||||||||||||
| let statement; | ||||||||||||||||||||||||||||||||||||||||||||
| db.aggregate('reenter_agg', { | ||||||||||||||||||||||||||||||||||||||||||||
| start: 0, | ||||||||||||||||||||||||||||||||||||||||||||
| step: mustCall((total, value) => { | ||||||||||||||||||||||||||||||||||||||||||||
| assert.throws(() => statement.get(), reentryError); | ||||||||||||||||||||||||||||||||||||||||||||
| return total + value; | ||||||||||||||||||||||||||||||||||||||||||||
| }, 3), | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| statement = db.prepare('SELECT reenter_agg(value) AS total FROM data'); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.deepStrictEqual(statement.get(), { __proto__: null, total: 6 }); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| test('reentry from an aggregate result is rejected', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const db = newDbWithRows(); | ||||||||||||||||||||||||||||||||||||||||||||
| let statement; | ||||||||||||||||||||||||||||||||||||||||||||
| db.aggregate('reenter_result', { | ||||||||||||||||||||||||||||||||||||||||||||
| start: 0, | ||||||||||||||||||||||||||||||||||||||||||||
| step: (total, value) => total + value, | ||||||||||||||||||||||||||||||||||||||||||||
| result: mustCall((total) => { | ||||||||||||||||||||||||||||||||||||||||||||
| assert.throws(() => statement.get(), reentryError); | ||||||||||||||||||||||||||||||||||||||||||||
| return total; | ||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| statement = db.prepare('SELECT reenter_result(value) AS total FROM data'); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.deepStrictEqual(statement.get(), { __proto__: null, total: 6 }); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iterate()also needs to mark the statement before reset/binding; otherwise a named-parameter getter can reenter and create a second iterator sharing the same VM and reset generation.