From 32365fa0735ee1f33018fdbe38509673add146e6 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Fri, 20 Nov 2026 12:53:26 -0800 Subject: [PATCH 1/4] ADFA-7200: Build sqlite-js 1.4.3 with upstream #36 as a patch Moves the package from 1.4.2-3 to 1.4.3, which adds #33 (sqlitejs_cancel_all, for apps that embed sqlite-js; not exported here), and carries upstream pull request #36 as 0001-*.patch until a release has it: - an error while building a call's arguments or a toArray() row fails the call, instead of running it with fewer arguments or a short row; - text that SQLite runs out of memory converting gives "out of memory" instead of crashing sqlite3; - js_create_scalar() and its siblings fail when the function cannot be stored in js_functions, for example on a read-only database. run-tests.sh gains three tests for the first and third, and expects js_version() 1.4.3. On a Linux host with /usr/bin/sqlite3 3.46.1, all 107 pass against 1.4.3 with the patch, and only the three new ones fail against 1.4.3 without it. The out-of-memory case needs a heap limit relative to the memory in use, which the shell cannot set; upstream's own test of it runs on the same patch in CodeOnTheGo's test.sh. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw --- README.md | 2 +- ...at-converting-values-and-storing-fun.patch | 292 ++++++++++++++++++ packages/sqlite-js/README.md | 16 +- packages/sqlite-js/build.sh | 12 +- packages/sqlite-js/tests/run-tests.sh | 21 +- 5 files changed, 332 insertions(+), 11 deletions(-) create mode 100644 packages/sqlite-js/0001-Report-errors-that-converting-values-and-storing-fun.patch diff --git a/README.md b/README.md index c6d0c5b..8240fa1 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Packages that are not in `termux-packages` live in `packages//`, laid out | Package | What it is | |---------|------------| | `sqlite-brotli` | SQLite extension adding `encodeBrotli()` and `decodeBrotli()`. See [its README](packages/sqlite-brotli/README.md). | -| `sqlite-js` | SQLite extension for SQL functions written in JavaScript: [sqliteai/sqlite-js](https://github.com/sqliteai/sqlite-js) 1.4.2, which includes our changes. See [its README](packages/sqlite-js/README.md). | +| `sqlite-js` | SQLite extension for SQL functions written in JavaScript: [sqliteai/sqlite-js](https://github.com/sqliteai/sqlite-js) 1.4.3, which includes our changes, with one patch of ours not yet released. See [its README](packages/sqlite-js/README.md). | The key specified with the `-s` option is the **public key** (**NOT** the private key!), which can be generated using : diff --git a/packages/sqlite-js/0001-Report-errors-that-converting-values-and-storing-fun.patch b/packages/sqlite-js/0001-Report-errors-that-converting-values-and-storing-fun.patch new file mode 100644 index 0000000..9422344 --- /dev/null +++ b/packages/sqlite-js/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/packages/sqlite-js/README.md b/packages/sqlite-js/README.md index 933ed1d..0616644 100644 --- a/packages/sqlite-js/README.md +++ b/packages/sqlite-js/README.md @@ -2,8 +2,9 @@ A SQLite loadable extension for writing SQL functions in JavaScript: scalar functions, aggregates, window functions and collations (ADFA-7081). It is -[sqliteai/sqlite-js](https://github.com/sqliteai/sqlite-js) 1.4.2, which -includes twelve changes of ours, listed [below](#changes-in-this-build). The +[sqliteai/sqlite-js](https://github.com/sqliteai/sqlite-js) 1.4.3, which +includes thirteen changes of ours, plus one patch of ours that upstream has not +released yet, listed [below](#changes-in-this-build). The JavaScript engine, [quickjs-ng](https://github.com/quickjs-ng/quickjs) 0.12.1, is built in. @@ -234,7 +235,16 @@ function could crash `sqlite3`, and a result set kept in a global kept a statement open: Ctrl-C during plain SQL then left every later statement failing with "interrupted". -This build carries no patches. +Upstream released one more in 1.4.3: #33 lets an app that embeds sqlite-js +cancel all running JavaScript with `sqlitejs_cancel_all()`. `sqlite3` does not +use it, and this build does not export it. + +This build adds one patch, `0001-*.patch`, which is upstream #36, until a +release has it (ADFA-7200): + +| Upstream PR | Change | +|-------------|--------| +| #36 | Errors that 1.4.3 drops are reported. An error while building a call's arguments, or a row of `toArray()`, fails the call; in 1.4.3 the function runs with fewer arguments, or gets a short row. Text that SQLite runs out of memory converting gives an "out of memory" error; in 1.4.3, `sqlite3` crashes. `js_create_scalar` and its siblings fail when they cannot store the function in `js_functions`, for example on a read-only database; in 1.4.3 they report success, and the function is gone in the next session. | The build also exports only the extension's entry points, and adds the one SQLite derives from the file name, so that `.load sqlite-js` needs no entry diff --git a/packages/sqlite-js/build.sh b/packages/sqlite-js/build.sh index ff3a297..128d954 100644 --- a/packages/sqlite-js/build.sh +++ b/packages/sqlite-js/build.sh @@ -3,13 +3,13 @@ TERMUX_PKG_DESCRIPTION="SQLite extension for functions written in JavaScript" TERMUX_PKG_LICENSE="Apache-2.0, MIT" TERMUX_PKG_LICENSE_FILE="LICENSE, LICENSE.quickjs" TERMUX_PKG_MAINTAINER="@appdevforall" -TERMUX_PKG_VERSION=1.4.2 -TERMUX_PKG_REVISION=3 +TERMUX_PKG_VERSION=1.4.3 TERMUX_PKG_SRCURL=https://github.com/sqliteai/sqlite-js/archive/refs/tags/${TERMUX_PKG_VERSION}.tar.gz -TERMUX_PKG_SHA256=1aa4c566491f1b56f3fd676b80880fdccbf0806b8f3cd015f60cd0f1c171cf24 -# Upstream released our changes (sqliteai/sqlite-js #11 to #19) in 1.4.0, and -# #22 to #24 in 1.4.2, so this build carries no patches. Patches of ours would -# go here as *.patch files, each offered upstream first. +TERMUX_PKG_SHA256=a2ed16f353757767188134f74fc9f0b07feefa96abfa56bcc211758b4105ad0a +# Upstream released our changes (sqliteai/sqlite-js #11 to #19) in 1.4.0, #22 +# to #24 in 1.4.2, and #33 in 1.4.3. 0001-*.patch is upstream #36, not yet +# released (ADFA-7200): drop it once a release has it. Patches of ours go here +# as *.patch files, each offered upstream first. TERMUX_PKG_BUILD_DEPENDS="libsqlite" TERMUX_PKG_RECOMMENDS="sqlite" TERMUX_PKG_BUILD_IN_SRC=true diff --git a/packages/sqlite-js/tests/run-tests.sh b/packages/sqlite-js/tests/run-tests.sh index a087dd7..d17e029 100644 --- a/packages/sqlite-js/tests/run-tests.sh +++ b/packages/sqlite-js/tests/run-tests.sh @@ -251,7 +251,7 @@ expect "loading the extension twice in one session is harmless" "2" \ # --- AC2: the engine is quickjs-ng -------------------------------------------- -expect "AC2: js_version() is the sqlite-js version, 1.4.2" "1.4.2" "SELECT js_version();" +expect "AC2: js_version() is the sqlite-js version, 1.4.3" "1.4.3" "SELECT js_version();" got=$(sql "SELECT js_version(1);" 2>&1) # Bellard's QuickJS has no version function, so sqlite-js does not build with it. if [[ $got =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then @@ -772,6 +772,25 @@ expect_error "AC14: a result set kept in a global throws when a later call reads "SELECT js_eval('globalThis.held = db.exec(\"SELECT 1 UNION ALL SELECT 2\"); held.next(); 0');" \ "SELECT js_eval('held.next()');" +# --- ADFA-7200: errors that 1.4.3 drops (upstream #36, patch 0001) ------------- + +# A setter on Array.prototype makes putting an element into a new array throw. +# The call must fail with that error, not run with fewer arguments or rows. +SETTER="SELECT js_eval('Object.defineProperty(Array.prototype, \"0\", { set(v) { throw new Error(\"index setter\"); }, configurable: true }); 1');" +expect_error "ADFA-7200: an error building a call's arguments fails the call" "index setter" \ + "SELECT js_create_scalar('first_arg', '(function (args) { return (args[0] === undefined) ? \"missing\" : args[0]; })');" \ + "$SETTER" "SELECT first_arg(5);" +expect_error "ADFA-7200: an error building a toArray() row fails the call" "index setter" \ + "$SETTER" "SELECT js_eval('db.exec(\"SELECT 1, 2\").toArray().length');" + +# On a read-only connection a new function is registered but cannot be stored +# in js_functions, so it would be gone in the next session: that is an error. +sql "SELECT js_init_table();" >/dev/null 2>&1 +expect_error "ADFA-7200: a function that cannot be stored in js_functions is an error" \ + "could not be stored in js_functions" \ + ".open --readonly $(dot_arg "$DB")" "$LOAD_EXT" \ + "SELECT js_create_scalar('not_stored', '(function (args) { return 42; })');" + echo "1..$n" if [[ $failed -ne 0 ]]; then echo "# FAILED $failed of $n" From d70b938cde2c246abc4eb130deb393ba8834c45e Mon Sep 17 00:00:00 2001 From: David Schachter Date: Fri, 20 Nov 2026 17:38:00 -0800 Subject: [PATCH 2/4] ADFA-7200: Fix AC9's global-memory test on 32-bit Android; note the rounding run-tests.sh's "memory held by a global counts until the global is cleared" failed on a Galaxy J7 (Android 9, arm), in 1.4.2-3 as well. The memory limit counts a block's usable size, and Android 9's 32-bit allocator (jemalloc) rounds a 60 MB block up to 64 MB (malloc_usable_size 67108864), the whole default limit. Nothing else could even be parsed until the block was collected. The test now holds 48 MB, an exact size class, so it checks what it is named for: 108 of 108 pass on the J7, and 107 of 107 on a Linux host. The README's Limits section says a large ArrayBuffer can count up to a quarter more than its length where the allocator rounds up. Revision 1, because the installed script and README changed and stage already has 1.4.3. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw --- packages/sqlite-js/README.md | 4 ++++ packages/sqlite-js/build.sh | 1 + packages/sqlite-js/tests/run-tests.sh | 5 ++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/sqlite-js/README.md b/packages/sqlite-js/README.md index 0616644..9650bc4 100644 --- a/packages/sqlite-js/README.md +++ b/packages/sqlite-js/README.md @@ -178,6 +178,10 @@ it as TEXT, `CAST(x AS TEXT)`, and convert it with `BigInt(args[0])`. `js_set_memory_limit` changes the limit, and collects garbage. Memory that globals still hold counts against the limit until they are cleared, so a limit under it leaves no room for any JavaScript until it is raised again. + The limit counts the memory the allocator actually hands out. On Android 10 + and older, whose allocator (jemalloc) rounds large blocks up to a size + class, a big `ArrayBuffer` can count up to a quarter more than its length: + a 60 MB one counts as 64 MB, the whole default limit. - **Endless code.** Ctrl-C stops running JavaScript: the statement fails with an "interrupted" error, even if the code catches exceptions. In an interactive session `sqlite3` keeps running; with SQL from a file or from diff --git a/packages/sqlite-js/build.sh b/packages/sqlite-js/build.sh index 128d954..2041124 100644 --- a/packages/sqlite-js/build.sh +++ b/packages/sqlite-js/build.sh @@ -4,6 +4,7 @@ TERMUX_PKG_LICENSE="Apache-2.0, MIT" TERMUX_PKG_LICENSE_FILE="LICENSE, LICENSE.quickjs" TERMUX_PKG_MAINTAINER="@appdevforall" TERMUX_PKG_VERSION=1.4.3 +TERMUX_PKG_REVISION=1 TERMUX_PKG_SRCURL=https://github.com/sqliteai/sqlite-js/archive/refs/tags/${TERMUX_PKG_VERSION}.tar.gz TERMUX_PKG_SHA256=a2ed16f353757767188134f74fc9f0b07feefa96abfa56bcc211758b4105ad0a # Upstream released our changes (sqliteai/sqlite-js #11 to #19) in 1.4.0, #22 diff --git a/packages/sqlite-js/tests/run-tests.sh b/packages/sqlite-js/tests/run-tests.sh index d17e029..1261f56 100644 --- a/packages/sqlite-js/tests/run-tests.sh +++ b/packages/sqlite-js/tests/run-tests.sh @@ -538,9 +538,12 @@ expect_nobail "AC9: code that keeps allocating fails at the limit, and the sessi "1,2,3" "out of memory" \ "SELECT js_eval('(function () { const a = []; for (let i = 0; i < 160; i++) a.push(new Array(100000).fill(1)); })()');" \ "SELECT js_eval('[1, 2, 3].join()');" +# 48 MB, not 60: the limit counts a block's usable size, and jemalloc (Android +# 10 and older) rounds a 60 MB block up to 64 MB, the whole limit, so nothing +# else could even be parsed until it was collected. 48 MB is an exact size class. expect_nobail "AC9: memory held by a global counts until the global is cleared" \ $'0\n16777216' "out of memory" \ - "SELECT js_eval('globalThis.keep = new ArrayBuffer(60 * 1024 * 1024); 0');" \ + "SELECT js_eval('globalThis.keep = new ArrayBuffer(48 * 1024 * 1024); 0');" \ "SELECT js_eval('new ArrayBuffer(16 * 1024 * 1024).byteLength');" \ "SELECT js_eval('keep = null; new ArrayBuffer(16 * 1024 * 1024).byteLength');" From 91421a2ce23152e75efe064fd23ca3609d6bfeaa Mon Sep 17 00:00:00 2001 From: David Schachter Date: Fri, 20 Nov 2026 17:41:05 -0800 Subject: [PATCH 3/4] ADFA-7200: Address review: claim only the allocator rounding that was measured The comment and the README said "Android 10 and older"; only Android 9 was measured. They now say "measured on a 32-bit Android 9 phone". "Up to a quarter" stays, now measured on that phone: malloc(32 MB + 4 KB) reports 40 MB usable (+25.0%), and malloc(8 MB + 4 KB) 10 MB (+24.9%). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw --- packages/sqlite-js/README.md | 9 +++++---- packages/sqlite-js/tests/run-tests.sh | 7 ++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/sqlite-js/README.md b/packages/sqlite-js/README.md index 9650bc4..5648994 100644 --- a/packages/sqlite-js/README.md +++ b/packages/sqlite-js/README.md @@ -178,10 +178,11 @@ it as TEXT, `CAST(x AS TEXT)`, and convert it with `BigInt(args[0])`. `js_set_memory_limit` changes the limit, and collects garbage. Memory that globals still hold counts against the limit until they are cleared, so a limit under it leaves no room for any JavaScript until it is raised again. - The limit counts the memory the allocator actually hands out. On Android 10 - and older, whose allocator (jemalloc) rounds large blocks up to a size - class, a big `ArrayBuffer` can count up to a quarter more than its length: - a 60 MB one counts as 64 MB, the whole default limit. + The limit counts the memory the allocator actually hands out. Where that is + jemalloc, which rounds large blocks up to a size class (measured on a 32-bit + Android 9 phone), a big `ArrayBuffer` can count up to a quarter more than its + length: 32 MB and 4 KB counts as 40 MB, and 60 MB as 64 MB, the whole + default limit. - **Endless code.** Ctrl-C stops running JavaScript: the statement fails with an "interrupted" error, even if the code catches exceptions. In an interactive session `sqlite3` keeps running; with SQL from a file or from diff --git a/packages/sqlite-js/tests/run-tests.sh b/packages/sqlite-js/tests/run-tests.sh index 1261f56..d6d8e5c 100644 --- a/packages/sqlite-js/tests/run-tests.sh +++ b/packages/sqlite-js/tests/run-tests.sh @@ -538,9 +538,10 @@ expect_nobail "AC9: code that keeps allocating fails at the limit, and the sessi "1,2,3" "out of memory" \ "SELECT js_eval('(function () { const a = []; for (let i = 0; i < 160; i++) a.push(new Array(100000).fill(1)); })()');" \ "SELECT js_eval('[1, 2, 3].join()');" -# 48 MB, not 60: the limit counts a block's usable size, and jemalloc (Android -# 10 and older) rounds a 60 MB block up to 64 MB, the whole limit, so nothing -# else could even be parsed until it was collected. 48 MB is an exact size class. +# 48 MB, not 60: the limit counts a block's usable size, and jemalloc (measured +# on a 32-bit Android 9 phone) rounds a 60 MB block up to 64 MB, the whole limit, +# so nothing else could even be parsed until it was collected. 48 MB is an exact +# jemalloc size class. expect_nobail "AC9: memory held by a global counts until the global is cleared" \ $'0\n16777216' "out of memory" \ "SELECT js_eval('globalThis.keep = new ArrayBuffer(48 * 1024 * 1024); 0');" \ From 645af052a9cbc4bb58a8c0cce5bd91b36ba00aa9 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sat, 21 Nov 2026 00:33:58 -0800 Subject: [PATCH 4/4] ADFA-7200: Build sqlite-js 1.4.4, which released upstream #36 Upstream merged #36 and released it in 1.4.4, so the patch goes and the revision starts again. 1.4.4's sources are 1.4.3 with the patch applied, byte for byte, except SQLITE_JS_VERSION. AC2 now expects 1.4.4; the ADFA-7200 tests stay. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw --- README.md | 2 +- ...at-converting-values-and-storing-fun.patch | 292 ------------------ packages/sqlite-js/README.md | 10 +- packages/sqlite-js/build.sh | 11 +- packages/sqlite-js/tests/run-tests.sh | 4 +- 5 files changed, 13 insertions(+), 306 deletions(-) delete mode 100644 packages/sqlite-js/0001-Report-errors-that-converting-values-and-storing-fun.patch diff --git a/README.md b/README.md index 8240fa1..fa8cd1d 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Packages that are not in `termux-packages` live in `packages//`, laid out | Package | What it is | |---------|------------| | `sqlite-brotli` | SQLite extension adding `encodeBrotli()` and `decodeBrotli()`. See [its README](packages/sqlite-brotli/README.md). | -| `sqlite-js` | SQLite extension for SQL functions written in JavaScript: [sqliteai/sqlite-js](https://github.com/sqliteai/sqlite-js) 1.4.3, which includes our changes, with one patch of ours not yet released. See [its README](packages/sqlite-js/README.md). | +| `sqlite-js` | SQLite extension for SQL functions written in JavaScript: [sqliteai/sqlite-js](https://github.com/sqliteai/sqlite-js) 1.4.4, which includes our changes. See [its README](packages/sqlite-js/README.md). | The key specified with the `-s` option is the **public key** (**NOT** the private key!), which can be generated using : diff --git a/packages/sqlite-js/0001-Report-errors-that-converting-values-and-storing-fun.patch b/packages/sqlite-js/0001-Report-errors-that-converting-values-and-storing-fun.patch deleted file mode 100644 index 9422344..0000000 --- a/packages/sqlite-js/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/packages/sqlite-js/README.md b/packages/sqlite-js/README.md index 5648994..925d824 100644 --- a/packages/sqlite-js/README.md +++ b/packages/sqlite-js/README.md @@ -2,9 +2,8 @@ A SQLite loadable extension for writing SQL functions in JavaScript: scalar functions, aggregates, window functions and collations (ADFA-7081). It is -[sqliteai/sqlite-js](https://github.com/sqliteai/sqlite-js) 1.4.3, which -includes thirteen changes of ours, plus one patch of ours that upstream has not -released yet, listed [below](#changes-in-this-build). The +[sqliteai/sqlite-js](https://github.com/sqliteai/sqlite-js) 1.4.4, which +includes fourteen changes of ours, listed [below](#changes-in-this-build). The JavaScript engine, [quickjs-ng](https://github.com/quickjs-ng/quickjs) 0.12.1, is built in. @@ -244,13 +243,14 @@ Upstream released one more in 1.4.3: #33 lets an app that embeds sqlite-js cancel all running JavaScript with `sqlitejs_cancel_all()`. `sqlite3` does not use it, and this build does not export it. -This build adds one patch, `0001-*.patch`, which is upstream #36, until a -release has it (ADFA-7200): +And one more in 1.4.4 (ADFA-7200): | Upstream PR | Change | |-------------|--------| | #36 | Errors that 1.4.3 drops are reported. An error while building a call's arguments, or a row of `toArray()`, fails the call; in 1.4.3 the function runs with fewer arguments, or gets a short row. Text that SQLite runs out of memory converting gives an "out of memory" error; in 1.4.3, `sqlite3` crashes. `js_create_scalar` and its siblings fail when they cannot store the function in `js_functions`, for example on a read-only database; in 1.4.3 they report success, and the function is gone in the next session. | +This build carries no patches. + The build also exports only the extension's entry points, and adds the one SQLite derives from the file name, so that `.load sqlite-js` needs no entry point argument. diff --git a/packages/sqlite-js/build.sh b/packages/sqlite-js/build.sh index 2041124..3f05c4f 100644 --- a/packages/sqlite-js/build.sh +++ b/packages/sqlite-js/build.sh @@ -3,14 +3,13 @@ TERMUX_PKG_DESCRIPTION="SQLite extension for functions written in JavaScript" TERMUX_PKG_LICENSE="Apache-2.0, MIT" TERMUX_PKG_LICENSE_FILE="LICENSE, LICENSE.quickjs" TERMUX_PKG_MAINTAINER="@appdevforall" -TERMUX_PKG_VERSION=1.4.3 -TERMUX_PKG_REVISION=1 +TERMUX_PKG_VERSION=1.4.4 TERMUX_PKG_SRCURL=https://github.com/sqliteai/sqlite-js/archive/refs/tags/${TERMUX_PKG_VERSION}.tar.gz -TERMUX_PKG_SHA256=a2ed16f353757767188134f74fc9f0b07feefa96abfa56bcc211758b4105ad0a +TERMUX_PKG_SHA256=0a6a196a1e487009a086608a9e72c4043ba6d548506ee8170795280614492642 # Upstream released our changes (sqliteai/sqlite-js #11 to #19) in 1.4.0, #22 -# to #24 in 1.4.2, and #33 in 1.4.3. 0001-*.patch is upstream #36, not yet -# released (ADFA-7200): drop it once a release has it. Patches of ours go here -# as *.patch files, each offered upstream first. +# to #24 in 1.4.2, #33 in 1.4.3 and #36 in 1.4.4, so this build carries no +# patches. Patches of ours would go here as *.patch files, each offered +# upstream first. TERMUX_PKG_BUILD_DEPENDS="libsqlite" TERMUX_PKG_RECOMMENDS="sqlite" TERMUX_PKG_BUILD_IN_SRC=true diff --git a/packages/sqlite-js/tests/run-tests.sh b/packages/sqlite-js/tests/run-tests.sh index d6d8e5c..d888820 100644 --- a/packages/sqlite-js/tests/run-tests.sh +++ b/packages/sqlite-js/tests/run-tests.sh @@ -251,7 +251,7 @@ expect "loading the extension twice in one session is harmless" "2" \ # --- AC2: the engine is quickjs-ng -------------------------------------------- -expect "AC2: js_version() is the sqlite-js version, 1.4.3" "1.4.3" "SELECT js_version();" +expect "AC2: js_version() is the sqlite-js version, 1.4.4" "1.4.4" "SELECT js_version();" got=$(sql "SELECT js_version(1);" 2>&1) # Bellard's QuickJS has no version function, so sqlite-js does not build with it. if [[ $got =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then @@ -776,7 +776,7 @@ expect_error "AC14: a result set kept in a global throws when a later call reads "SELECT js_eval('globalThis.held = db.exec(\"SELECT 1 UNION ALL SELECT 2\"); held.next(); 0');" \ "SELECT js_eval('held.next()');" -# --- ADFA-7200: errors that 1.4.3 drops (upstream #36, patch 0001) ------------- +# --- ADFA-7200: errors that 1.4.3 drops (upstream #36, in 1.4.4) -------------- # A setter on Array.prototype makes putting an element into a new array throw. # The call must fail with that error, not run with fewer arguments or rows.