Skip to content

Start offset (negative number) is outside the bounds of the buffer #270

@lexidor

Description

@lexidor

This is a wontfix, if you see this error, your application is leaking memory.
Suggestion for the next developer reading this, finalize your prepared statements.

RangeError: Start offset -1169154640 is outside the bounds of the buffer
    at new Uint8Array (<anonymous>)
    at setStr (https://deno.land/x/sqlite@v3.9.1/src/wasm.ts:15:15)
    at PreparedQuery.#startQuery (https://deno.land/x/sqlite@v3.9.1/src/query.ts:232:20)
    at PreparedQuery.execute (https://deno.land/x/sqlite@v3.9.1/src/query.ts:629:21)

Now with a reproducer. This error occurs when sqlite needs more memory than the host buffer is able to provide. The reason why this threw in my application is because I leaked prepared statements.

import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts";

const dbFile = "sqlite3.db";
Deno.remove(dbFile).catch();

console.log("Opening DB:", dbFile);
const db = new DB(dbFile);
db.execute(
  `CREATE TABLE IF NOT EXISTS mytable (
     id INTEGER PRIMARY KEY,
     megabyte TEXT
  )`,
);

let inserted = 0;
const logEvery = 100;

console.log("Starting inserts (leaking prepared statement each iteration).");
const megabyte = "a".repeat(1024 * 1024);

while (true) {
  const insertSql = "INSERT INTO mytable (megabyte) VALUES (?)";
  const stmt = db.prepareQuery(insertSql);
  stmt.execute([megabyte]);

  // If you don't invoke stmt.finalize here...

  inserted++;
  if (inserted % logEvery === 0) {
    console.log("Inserted:", inserted);
  }
}
Opening DB: sqlite3.db
Starting inserts (leaking prepared statement each iteration).
Inserted: 100
...
Inserted: 2300
error: Uncaught (in promise) RangeError: Start offset -1831501632 is outside the bounds of the buffer
  const mem = new Uint8Array(wasm.memory.buffer, ptr, bytes.length + 1);
              ^
    at new Uint8Array (<anonymous>)
    at setStr (https://deno.land/x/sqlite@v3.9.1/src/wasm.ts:15:15)
    at PreparedQuery.#startQuery (https://deno.land/x/sqlite@v3.9.1/src/query.ts:232:20)
    at PreparedQuery.execute (https://deno.land/x/sqlite@v3.9.1/src/query.ts:629:21)
    at file:///path/to/repro.ts:24:8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions