diff --git a/subprojects/sqlite-js/README.md b/subprojects/sqlite-js/README.md index 7fff743dba..50417ba803 100644 --- a/subprojects/sqlite-js/README.md +++ b/subprojects/sqlite-js/README.md @@ -6,8 +6,9 @@ An AAR that lets apps built with Code on the Go define and call JavaScript funct ## Contents -- `upstream/`: the sqlite-js 1.4.3 release (Apache-2.0), which has all our changes (upstream pull requests #11 to #19, #22 to #24, and #33, which added `sqlitejs_cancel_all()`), with QuickJS amalgamated in `libs/quickjs.c` (MIT). Only the files the build uses, plus `LICENSE` and `LICENSE.quickjs`. -- `patches/`, when there is one: our changes not yet upstream, which `vendor.sh` and `test.sh` apply in order. There are none now. +- `upstream/`: the sqlite-js 1.4.4 release (Apache-2.0), which has all our changes (upstream pull requests #11 to #19, #22 to #24, #33, which added `sqlitejs_cancel_all()`, and #36), with QuickJS amalgamated in `libs/quickjs.c` (MIT). Only the files the build uses, plus `LICENSE` and `LICENSE.quickjs`. +- `patches/`, when there is one: our changes not yet upstream, which `vendor.sh` and `test.sh` apply in order. Drop each one once a release has it. There are none now. +- Since 1.4.4 (upstream #36, ADFA-7200) a call fails, instead of running with fewer arguments or a short `toArray()` row, when building one of them fails. Text that SQLite runs out of memory converting gives an "out of memory" error instead of a crash in `strlen`. `js_create_scalar()` and its siblings report an error when the function can't be stored in `js_functions`, for example on a read-only connection, instead of reporting success. - Since 1.4.2 a `db.exec` result set does not outlive the call into JavaScript that created it: every one still open is ended when the call returns, because its statement keeps a read transaction open, keeps an interrupt in effect and stops the connection from closing. Reading one afterwards throws "The result set was closed when the call into JavaScript that created it returned", or "interrupted" when a cancel (or, on SQLite 3.41 and later, `sqlite3_interrupt()`) stopped the call. Ending them runs the finals of the window and aggregate functions they were computing, and a cancel during the call or while they are ended stops each final at QuickJS's next interrupt check. One read to its end still reports the end of its rows. - `src/main/cpp/`: the CMake build of `libsqlitejs.so`, and `sqlitejs_android.c`, which is ours: JNI for `SqliteJs`, and the two entry points it names. The version script exports only the entry points (and upstream's `sqlite3_js_init`) and `JNI_OnLoad`. - `src/test/c/glue_test.c` and `test.sh`: the tests the Android build cannot run. See Testing. diff --git a/subprojects/sqlite-js/upstream/src/sqlitejs.c b/subprojects/sqlite-js/upstream/src/sqlitejs.c index 538f4f2acd..657ef02f73 100644 --- a/subprojects/sqlite-js/upstream/src/sqlitejs.c +++ b/subprojects/sqlite-js/upstream/src/sqlitejs.c @@ -225,13 +225,23 @@ static JSValue js_rowset_to_array(JSContext *ctx, JSValueConst this_val, int arg return JS_EXCEPTION; } + // a value that cannot be converted or stored (out of memory, or a setter a script put on + // Array.prototype) is an exception, not a shorter row or a shorter array: JS_SetPropertyUint32 + // frees the value it is given, also when it fails for (int i=0; ivm, i); JSValue jvalue = sqlite_value_to_js(ctx, value); - JS_SetPropertyUint32(ctx, row, i, jvalue); + if (JS_IsException(jvalue) || (JS_SetPropertyUint32(ctx, row, i, jvalue) < 0)) { + JS_FreeValue(ctx, row); + JS_FreeValue(ctx, result); + return JS_EXCEPTION; + } } - JS_SetPropertyUint32(ctx, result, nrows++, row); + if (JS_SetPropertyUint32(ctx, result, nrows++, row) < 0) { + JS_FreeValue(ctx, result); + return JS_EXCEPTION; + } } // as in js_rowset_next: an error is an exception, not a shorter array @@ -1334,8 +1344,12 @@ static JSValue sqlite_value_to_js (JSContext *ctx, sqlite3_value *value) { return JS_NewFloat64(ctx, sqlite3_value_double(value)); break; - case SQLITE_TEXT: - return JS_NewString(ctx, (const char *)sqlite3_value_text(value)); + case SQLITE_TEXT: { + // NULL when SQLite runs out of memory converting the text to UTF-8 + const char *text = (const char *)sqlite3_value_text(value); + if (!text) return JS_ThrowOutOfMemory(ctx); + return JS_NewString(ctx, text); + } break; case SQLITE_BLOB: { @@ -1367,16 +1381,21 @@ static char *sqlite_strdup (const char *str) { // MARK: - Execution - static void js_execute_common (sqlite3_context *context, JSContext *js_context, int nvalues, sqlite3_value **values, JSValue func, JSValue this_obj, bool return_value) { - // create JS array for arguments + // create JS array for arguments: an argument that cannot be converted or stored (out of memory, + // or a setter a script put on Array.prototype) fails the call instead of leaving it out, and + // JS_SetPropertyUint32 frees the value it is given, also when it fails JSValue args = (values) ? JS_NewArray(js_context) : JS_NULL; - for (int i=0; i