Skip to content

Commit cba5a0f

Browse files
authored
fix: allocate SQL on the heap in Database.run and Database.prepare (#639)
Both passed the SQL to SQLite as a cwrap string, which Emscripten copies onto the 5 MB WebAssembly stack. SQL longer than that overwrote memory and left the database unusable. exec() and the statement iterator already allocate the SQL on the heap, so do the same here.
1 parent 07d3880 commit cba5a0f

2 files changed

Lines changed: 61 additions & 11 deletions

File tree

‎src/api.js‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,9 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
7777
var sqlite3_exec = cwrap(
7878
"sqlite3_exec",
7979
"number",
80-
["number", "string", "number", "number", "number"]
80+
["number", "number", "number", "number", "number"]
8181
);
8282
var sqlite3_changes = cwrap("sqlite3_changes", "number", ["number"]);
83-
var sqlite3_prepare_v2 = cwrap(
84-
"sqlite3_prepare_v2",
85-
"number",
86-
["number", "string", "number", "number", "number"]
87-
);
8883
var sqlite3_sql = cwrap("sqlite3_sql", "string", ["number"]);
8984
var sqlite3_normalized_sql = cwrap(
9085
"sqlite3_normalized_sql",
@@ -869,7 +864,12 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
869864
stmt.free();
870865
}
871866
} else {
872-
this.handleError(sqlite3_exec(this.db, sql, 0, 0, apiTemp));
867+
var sqlPtr = stringToNewUTF8(sql);
868+
try {
869+
this.handleError(sqlite3_exec(this.db, sqlPtr, 0, 0, apiTemp));
870+
} finally {
871+
_free(sqlPtr);
872+
}
873873
}
874874
return this;
875875
};
@@ -1048,10 +1048,15 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
10481048
@throws {String} SQLite error
10491049
*/
10501050
Database.prototype.prepare = function prepare(sql, params) {
1051-
setValue(apiTemp, 0, "i32");
1052-
this.handleError(
1053-
sqlite3_prepare_v2(this.db, sql, -1, apiTemp, NULL)
1054-
);
1051+
var sqlPtr = stringToNewUTF8(sql);
1052+
try {
1053+
setValue(apiTemp, 0, "i32");
1054+
this.handleError(
1055+
sqlite3_prepare_v2_sqlptr(this.db, sqlPtr, -1, apiTemp, NULL)
1056+
);
1057+
} finally {
1058+
_free(sqlPtr);
1059+
}
10551060
// pointer to a statement, or null
10561061
var pStmt = getValue(apiTemp, "i32");
10571062
if (pStmt === NULL) {

‎test/test_issue482.js‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// test for https://github.com/sql-js/sql.js/issues/482
2+
exports.test = function (sql, assert) {
3+
"use strict";
4+
// The WebAssembly stack is 5 MB, so SQL of this size cannot live on it
5+
var text = "a".repeat(6 * 1024 * 1024);
6+
var insert = "INSERT INTO t VALUES ('" + text + "', ?)";
7+
var lengths = "SELECT length(a) FROM t";
8+
9+
var db = new sql.Database();
10+
db.run("CREATE TABLE t (a, b)");
11+
12+
db.run("INSERT INTO t VALUES ('" + text + "', 1)");
13+
db.run(insert, [2]);
14+
var stmt = db.prepare(insert, [3]);
15+
stmt.step();
16+
stmt.free();
17+
18+
assert.deepEqual(
19+
db.exec(lengths)[0].values,
20+
[[text.length], [text.length], [text.length]],
21+
"long SQL passed to run, run with params and prepare is executed"
22+
);
23+
assert.deepEqual(
24+
db.exec("SELECT b FROM t")[0].values,
25+
[[1], [2], [3]],
26+
"database is still usable after long SQL"
27+
);
28+
29+
db.close();
30+
};
31+
32+
if (module == require.main) {
33+
const target_file = process.argv[2];
34+
const sql_loader = require('./load_sql_lib');
35+
sql_loader(target_file).then((sql) => {
36+
require('test').run({
37+
'test issue482': function (assert) {
38+
exports.test(sql, assert);
39+
}
40+
});
41+
}).catch((e) => {
42+
console.error(e);
43+
assert.fail(e);
44+
});
45+
}

0 commit comments

Comments
 (0)