From b0e0a04190a9121bed7a41b6a0fe4a239d08858e Mon Sep 17 00:00:00 2001 From: David Schachter Date: Fri, 20 Nov 2026 12:48:08 -0800 Subject: [PATCH 1/2] ADFA-7200: Patch sqlite-js 1.4.3 with upstream #36 CodeRabbit found three defects in sqlite-js 1.4.3. Upstream pull request #36 fixes them, but it has had no review since 2026-11-17, so the AAR carries it as patches/0001 until a release has it: - an error while building a call's arguments or a toArray() row is no longer ignored, so a function can't run with fewer arguments; - text that SQLite runs out of memory converting gives "out of memory" instead of a crash in strlen; - js_create_scalar() and its siblings report an error when the function can't be stored, instead of success. vendor.sh regenerated upstream/src/sqlitejs.c, and vendor.sh --check passes. test.sh passes, with #36's new Conversion errors and Store errors tests. Without the source change both fail, and with only the out-of-memory check removed the test program crashes in Conversion errors. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw --- subprojects/sqlite-js/README.md | 5 +- ...at-converting-values-and-storing-fun.patch | 292 ++++++++++++++++++ subprojects/sqlite-js/upstream/src/sqlitejs.c | 51 ++- 3 files changed, 336 insertions(+), 12 deletions(-) create mode 100644 subprojects/sqlite-js/patches/0001-Report-errors-that-converting-values-and-storing-fun.patch diff --git a/subprojects/sqlite-js/README.md b/subprojects/sqlite-js/README.md index 7fff743dba..726692b269 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.3 release (Apache-2.0), which has our earlier 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), and `patches/` applied. 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 (ADFA-7200). Drop each one once a release has it. + - `0001`: upstream pull request #36. 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. It also adds upstream's Conversion errors and Store errors tests, and counts its Memory check in the test program's exit code. - 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/patches/0001-Report-errors-that-converting-values-and-storing-fun.patch b/subprojects/sqlite-js/patches/0001-Report-errors-that-converting-values-and-storing-fun.patch new file mode 100644 index 0000000000..9422344b51 --- /dev/null +++ b/subprojects/sqlite-js/patches/0001-Report-errors-that-converting-values-and-storing-fun.patch @@ -0,0 +1,292 @@ +From 543bb4b1e7b4622772632a9b678959ef1338e0fd Mon Sep 17 00:00:00 2001 +From: David Schachter +Date: Fri, 20 Nov 2026 12:42:34 -0800 +Subject: [PATCH] Report errors that converting values and storing functions + dropped + +The fix in sqliteai/sqlite-js#36, carried until a release has it: + +- An error while building a call's arguments, or a row of toArray(), + was ignored, so the function ran with fewer arguments or a short row. + It now fails the call. +- When SQLite ran out of memory converting text to UTF-8, the NULL it + returned crashed the call in strlen. It now fails with "out of + memory". +- js_create_scalar(), js_create_aggregate() and the others reported + success when js_functions could not store the function, for example + on a read-only connection. They now report the storage error. + +The test program gains a Conversion errors and a Store errors test, the +first requiring SQLITE_NOMEM, and counts its Memory check in its exit +code. + +Co-Authored-By: Claude Opus 5.5 +Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw +--- + src/sqlitejs.c | 51 ++++++++++++++---- + test/main.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++- + 2 files changed, 181 insertions(+), 11 deletions(-) + +diff --git a/src/sqlitejs.c b/src/sqlitejs.c +index 538f4f2..657ef02 100644 +--- a/src/sqlitejs.c ++++ b/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 0) { + printf("memory leak: %lld\n", current); + } +- test_report("Memory", (int)current); ++ result += test_report("Memory", (int)current); + + return result; + } +-- +2.53.0 + 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 Date: Sat, 21 Nov 2026 00:33:58 -0800 Subject: [PATCH 2/2] ADFA-7200: Move to sqlite-js 1.4.4, which released upstream #36 Upstream merged #36 and released it in 1.4.4, so the patch goes and upstream/ is the plain release. Its sources are 1.4.3 with the patch applied, byte for byte, except SQLITE_JS_VERSION ("1.4.4"). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw --- subprojects/sqlite-js/README.md | 6 +- ...at-converting-values-and-storing-fun.patch | 292 ------------------ subprojects/sqlite-js/upstream/src/sqlitejs.h | 2 +- subprojects/sqlite-js/vendor.sh | 4 +- 4 files changed, 6 insertions(+), 298 deletions(-) delete mode 100644 subprojects/sqlite-js/patches/0001-Report-errors-that-converting-values-and-storing-fun.patch diff --git a/subprojects/sqlite-js/README.md b/subprojects/sqlite-js/README.md index 726692b269..50417ba803 100644 --- a/subprojects/sqlite-js/README.md +++ b/subprojects/sqlite-js/README.md @@ -6,9 +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 our earlier 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), and `patches/` applied. 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 (ADFA-7200). Drop each one once a release has it. - - `0001`: upstream pull request #36. 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. It also adds upstream's Conversion errors and Store errors tests, and counts its Memory check in the test program's exit code. +- `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/patches/0001-Report-errors-that-converting-values-and-storing-fun.patch b/subprojects/sqlite-js/patches/0001-Report-errors-that-converting-values-and-storing-fun.patch deleted file mode 100644 index 9422344b51..0000000000 --- a/subprojects/sqlite-js/patches/0001-Report-errors-that-converting-values-and-storing-fun.patch +++ /dev/null @@ -1,292 +0,0 @@ -From 543bb4b1e7b4622772632a9b678959ef1338e0fd Mon Sep 17 00:00:00 2001 -From: David Schachter -Date: Fri, 20 Nov 2026 12:42:34 -0800 -Subject: [PATCH] Report errors that converting values and storing functions - dropped - -The fix in sqliteai/sqlite-js#36, carried until a release has it: - -- An error while building a call's arguments, or a row of toArray(), - was ignored, so the function ran with fewer arguments or a short row. - It now fails the call. -- When SQLite ran out of memory converting text to UTF-8, the NULL it - returned crashed the call in strlen. It now fails with "out of - memory". -- js_create_scalar(), js_create_aggregate() and the others reported - success when js_functions could not store the function, for example - on a read-only connection. They now report the storage error. - -The test program gains a Conversion errors and a Store errors test, the -first requiring SQLITE_NOMEM, and counts its Memory check in its exit -code. - -Co-Authored-By: Claude Opus 5.5 -Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw ---- - src/sqlitejs.c | 51 ++++++++++++++---- - test/main.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++- - 2 files changed, 181 insertions(+), 11 deletions(-) - -diff --git a/src/sqlitejs.c b/src/sqlitejs.c -index 538f4f2..657ef02 100644 ---- a/src/sqlitejs.c -+++ b/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 0) { - printf("memory leak: %lld\n", current); - } -- test_report("Memory", (int)current); -+ result += test_report("Memory", (int)current); - - return result; - } --- -2.53.0 - diff --git a/subprojects/sqlite-js/upstream/src/sqlitejs.h b/subprojects/sqlite-js/upstream/src/sqlitejs.h index 9d54f92281..ea67cd90a0 100644 --- a/subprojects/sqlite-js/upstream/src/sqlitejs.h +++ b/subprojects/sqlite-js/upstream/src/sqlitejs.h @@ -16,7 +16,7 @@ #include "sqlite3.h" #endif -#define SQLITE_JS_VERSION "1.4.3" +#define SQLITE_JS_VERSION "1.4.4" int sqlite3_js_init (sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi); const char *sqlitejs_version (void); diff --git a/subprojects/sqlite-js/vendor.sh b/subprojects/sqlite-js/vendor.sh index b7bc0d3725..aeb8587479 100755 --- a/subprojects/sqlite-js/vendor.sh +++ b/subprojects/sqlite-js/vendor.sh @@ -2,8 +2,8 @@ # Rebuilds upstream/ from the sqlite-js release VERSION and any patches/, or checks that it matches. # Usage: vendor.sh [--check] [TARBALL] (without TARBALL, downloads it) set -euo pipefail -VERSION=1.4.3 -SHA256=a2ed16f353757767188134f74fc9f0b07feefa96abfa56bcc211758b4105ad0a +VERSION=1.4.4 +SHA256=0a6a196a1e487009a086608a9e72c4043ba6d548506ee8170795280614492642 URL=https://github.com/sqliteai/sqlite-js/archive/refs/tags/$VERSION.tar.gz HERE=$(cd "$(dirname "$0")" && pwd) CHECK=false