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; }