Skip to content

Commit f0e0e17

Browse files
fix table update notifications use after free
1 parent f9f5c84 commit f0e0e17

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

.changeset/stale-cups-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@journeyapps/wa-sqlite': patch
3+
---
4+
5+
Fix bug where table change update notifications would access invalid memory locations under certain conditions.

src/sqlite-api.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const async = true;
2929

3030
const onTableChangeCallbacks = {};
3131
globalThis.__onTablesChanged = function(db, opType, tableName, rowId) {
32-
setTimeout(() => onTableChangeCallbacks[db]?.(opType, tableName, rowId), 0);
32+
onTableChangeCallbacks[db]?.(opType, tableName, rowId);
3333
};
3434

3535
/**
@@ -914,7 +914,12 @@ export function Factory(Module) {
914914
const stringBytes = new Uint8Array(Module.HEAPU8.buffer, tableNamePtr, length);
915915
const tableName = new TextDecoder().decode(stringBytes);
916916

917-
return callback(opType, tableName, rowId);
917+
/**
918+
* Call the callback inside a setTimeout to avoid blocking SQLite.
919+
* We use a setTimeout only after fetching data from the heap to avoid
920+
* accessing memory which has been freed.
921+
*/
922+
setTimeout(() => callback(opType, tableName, rowId), 0)
918923
};
919924
};
920925

0 commit comments

Comments
 (0)