From 3d8e8eb352f68c822670335605b35ecdae78996f Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 13:35:16 -0800 Subject: [PATCH 1/3] Report errors that converting values and storing functions used to drop Three results were ignored: - Putting a value into the array of a call's arguments, or of a row of toArray(), can fail: QuickJS runs out of memory, or a script has put a setter on Array.prototype. The function then ran with fewer arguments, and toArray() returned shorter rows, with the exception left pending. Now the call fails with that exception, before the function runs or another row is read. - sqlite3_value_text() returns NULL when SQLite runs out of memory converting text to UTF-8, and JS_NewString() passed it to strlen(), which crashed. Now it is an "out of memory" error. - js_create_*() reported success when js_functions could not store the function (on a read-only connection, for example), so it was gone when the database was reopened. Now it is an error that says the function was registered on this connection but not stored, with SQLite's code. The new tests "Conversion errors" and "Store errors" fail without these changes; the text conversion case crashed. This change originates with AI: it was written by Claude (Anthropic's model) for App Dev For All. Contact: davidschachter@appdevforall.org. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw --- src/sqlitejs.c | 51 ++++++++++++++---- test/main.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+), 10 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 Date: Tue, 17 Nov 2026 13:35:16 -0800 Subject: [PATCH 2/3] Count the memory check in the test program's exit code main() printed the result of its final "Memory" check (memory still in use after every test) but left it out of the value it returns, so a leak alone let the run exit 0 and pass CI. This change originates with AI: it was written by Claude (Anthropic's model) for App Dev For All. Contact: davidschachter@appdevforall.org. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw --- test/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/main.c b/test/main.c index 5b9c148..c930ead 100644 --- a/test/main.c +++ b/test/main.c @@ -1636,7 +1636,7 @@ int main (void) { if (current > 0) { printf("memory leak: %lld\n", current); } - test_report("Memory", (int)current); + result += test_report("Memory", (int)current); return result; } From 18522e8bb672bc82121cf9f21f848a1ce488b716 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 13:37:10 -0800 Subject: [PATCH 3/3] Address review: require out of memory in the text conversion test - The text conversion case now passes only when the statement fails with SQLITE_NOMEM, so a failure for any other reason cannot pass it without reaching the conversion. - The UTF-16 path for sqlite3_open16() is spelled as an array, since a u"" literal needs C11. This change originates with AI: it was written by Claude (Anthropic's model) for App Dev For All. Contact: davidschachter@appdevforall.org. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw --- test/main.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/main.c b/test/main.c index c930ead..1b4c366 100644 --- a/test/main.c +++ b/test/main.c @@ -890,7 +890,8 @@ int test_numbers (void) { // Opens an in-memory database with the extension loaded whose text is UTF-16, as sqlite3_open16() // makes it, so that a UTF-8 function reading its text makes SQLite convert and allocate. static int test_open_utf16 (sqlite3 **db) { - int rc = sqlite3_open16(u":memory:", db); + static const unsigned short memory16[] = {':', 'm', 'e', 'm', 'o', 'r', 'y', ':', 0}; + int rc = sqlite3_open16(memory16, db); if (rc != SQLITE_OK) return rc; #if JS_LOAD_EMBEDDED @@ -920,8 +921,9 @@ static int test_text_without_memory (sqlite3 *db) { sqlite3_hard_heap_limit64(sqlite3_memory_used() + 256 * 1024); rc = sqlite3_step(stmt); sqlite3_hard_heap_limit64(hard_limit); - if (rc == SQLITE_ROW) { - printf("FAIL: text_length() of 1M characters ran with no memory for their UTF-8 copy\n"); + // only the lack of memory for the copy may stop it: any other error would not test the copy + if ((rc & 0xff) != SQLITE_NOMEM) { + printf("FAIL: text_length() of 1M characters with no memory for their UTF-8 copy\n Expected: SQLITE_NOMEM, Got: %d %s\n", rc, (rc == SQLITE_ROW) ? "a row" : sqlite3_errmsg(db)); rc = SQLITE_ERROR; goto cleanup; }