Skip to content

sqlite: statement iterator state is corrupted by coincident statement execution #61819

Description

@Renegade334

Statement iterators that have not finished iterating are not invalidated when the statement is reset.

const { DatabaseSync } = require('node:sqlite');
const db = new DatabaseSync(':memory:')
db.exec('CREATE TABLE test (value INTEGER NOT NULL)');
for (let i = 0; i < 10; i++) {
  db.prepare('INSERT INTO test (value) VALUES (?)').run(i);
}

const stmt = db.prepare('SELECT * FROM test');

const it = stmt.iterate();
it.next().value; // { value: 0 }
it.next().value; // { value: 1 }
it.next().value; // { value: 2 }

// resets, executes and re-resets statement
stmt.get(); // { value: 0 }

// sqlite3_step restarts from beginning of result
it.next().value; // { value: 0 }
it.next().value; // { value: 1 }
it.next().value; // { value: 2 }

// these will race
const it2 = stmt.iterate();
it.next().value; // { value: 0 }
it2.next().value; // { value: 1 }
it.next().value; // { value: 2 }
it2.next().value; // { value: 3 }

If the statement iterator is not done and the statement is reset, then the iterator should be invalidated and subsequent calls to .next() should throw.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    sqliteIssues and PRs related to the SQLite subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions