Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions src/sqlitejs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; i<ncols; ++i) {
sqlite3_value *value = sqlite3_column_value(rs->vm, 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
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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<nvalues; ++i) {
for (int i=0; (i<nvalues) && !JS_IsException(args); ++i) {
JSValue js_val = sqlite_value_to_js(js_context, values[i]);
JS_SetPropertyUint32(js_context, args, i, js_val);
if (JS_IsException(js_val) || (JS_SetPropertyUint32(js_context, args, i, js_val) < 0)) {
JS_FreeValue(js_context, args);
args = JS_EXCEPTION;
}
}

// call the JavaScript function with args_array as the argument
JSValueConst args_val[] = {args};
JSValue result = JS_Call(js_context, func, this_obj, (values) ? 1 : 0, (values) ? args_val : NULL);
JSValue result = (JS_IsException(args)) ? JS_EXCEPTION : JS_Call(js_context, func, this_obj, (values) ? 1 : 0, (values) ? args_val : NULL);
JS_FreeValue(js_context, args);

if (return_value) js_value_to_sqlite(context, js_context, result);
Expand Down Expand Up @@ -1664,8 +1683,20 @@ static bool js_create_function (sqlite3_context *context, const char *type, cons
// so for collations the function context has to be released here
if ((rc != SQLITE_OK) && is_collation) functionjs_free(fctx);

if ((is_load == false) && (rc == SQLITE_OK)) {
js_add_to_table(context, type, name, init_code, step_code, final_code, value_code, inverse_code);
// a function that is registered but not stored (on a read-only connection, for example) would
// be gone when the database is reopened, so it is an error even though this connection has it
if ((is_load == false) && (rc == SQLITE_OK) && !js_add_to_table(context, type, name, init_code, step_code, final_code, value_code, inverse_code)) {
sqlite3 *db = sqlite3_context_db_handle(context);
int code = sqlite3_errcode(db);
char *msg = sqlite3_mprintf("The function was registered on this connection, but it could not be stored in js_functions: %s", sqlite3_errmsg(db));
if (!msg) {
sqlite3_result_error_nomem(context);
return false;
}
sqlite3_result_error(context, msg, -1);
sqlite3_free(msg);
sqlite3_result_error_code(context, (code != SQLITE_OK) ? code : SQLITE_ERROR);
return false;
}

// js_execute_cleanup is automatically called in case of error (collations excluded, see above)
Expand Down
141 changes: 140 additions & 1 deletion test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,143 @@ int test_numbers (void) {
return rc;
}

// MARK: - Conversion errors -

// 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) {
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
return sqlite3_js_init(*db, NULL, NULL);
#else
rc = sqlite3_enable_load_extension(*db, 1);
if (rc != SQLITE_OK) return rc;
return db_exec(*db, "SELECT load_extension('./dist/js');");
#endif
}

// Runs text_length(?1) on 1M characters of UTF-16 with too little memory left for their UTF-8
// copy: the call must fail, not hand the NULL that sqlite3_value_text() returns to JavaScript.
static int test_text_without_memory (sqlite3 *db) {
const int nchars = 1024 * 1024;
sqlite3_stmt *stmt = NULL;
unsigned short *text = malloc((size_t)nchars * sizeof(unsigned short));
if (!text) return SQLITE_NOMEM;
for (int i=0; i<nchars; ++i) text[i] = 'a';

int rc = sqlite3_prepare_v2(db, "SELECT text_length(?1);", -1, &stmt, NULL);
if (rc == SQLITE_OK) rc = sqlite3_bind_text16(stmt, 1, text, nchars * (int)sizeof(unsigned short), SQLITE_TRANSIENT);
free(text);
if (rc != SQLITE_OK) goto cleanup;

sqlite3_int64 hard_limit = sqlite3_hard_heap_limit64(-1);
sqlite3_hard_heap_limit64(sqlite3_memory_used() + 256 * 1024);
rc = sqlite3_step(stmt);
sqlite3_hard_heap_limit64(hard_limit);
// 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;
}
DEBUGF("PASS: text_length() failed with no memory for its argument's UTF-8 copy: %d %s\n", rc, sqlite3_errmsg(db));
rc = SQLITE_OK;

cleanup:
sqlite3_finalize(stmt);
return rc;
}

// An error while the arguments of a call or the rows of toArray() are put into arrays stops the
// call: a function never runs with fewer arguments, and toArray() never returns fewer columns.
int test_conversion_errors (void) {
sqlite3 *db = NULL;
int rc = test_open(&db);
if (rc != SQLITE_OK) goto abort_test;

rc = db_exec(db, "SELECT js_create_scalar('first_arg', '(function(args){ return (args[0] === undefined) ? \"missing\" : args[0]; })');");
if (rc != SQLITE_OK) goto abort_test;

// a setter on Array.prototype makes putting an element into a new array throw
rc = db_exec(db, "SELECT js_eval('Object.defineProperty(Array.prototype, \"0\", { set(v) { throw new Error(\"index setter\"); }, configurable: true }); 1');");
if (rc != SQLITE_OK) goto abort_test;
rc = check_error(db, "SELECT first_arg(5);", "index setter");
if (rc != SQLITE_OK) goto abort_test;
rc = check_error(db, "SELECT js_eval('db.exec(\"SELECT 1, 2\").toArray().length');", "index setter");
if (rc != SQLITE_OK) goto abort_test;

// without the setter both work again
rc = db_exec(db, "SELECT js_eval('delete Array.prototype[0]; 1');");
if (rc != SQLITE_OK) goto abort_test;
rc = check_query_v2(db, "SELECT first_arg(5), js_eval('JSON.stringify(db.exec(\"SELECT 1, 2\").toArray())');", (const char*[]){"5", "[[1,2]]"}, 2);
if (rc != SQLITE_OK) goto abort_test;
sqlite3_close(db);
db = NULL;

rc = test_open_utf16(&db);
if (rc != SQLITE_OK) goto abort_test;
rc = db_exec(db, "SELECT js_create_scalar('text_length', '(function(args){ return args[0].length; })');");
if (rc != SQLITE_OK) goto abort_test;
rc = check_query(db, "SELECT text_length('abc');", "3");
if (rc != SQLITE_OK) goto abort_test;
rc = test_text_without_memory(db);
if (rc != SQLITE_OK) goto abort_test;
rc = check_query(db, "SELECT text_length('abcd');", "4");
if (rc != SQLITE_OK) goto abort_test;

abort_test:
if (rc != SQLITE_OK && db) printf("Error: %s\n", sqlite3_errmsg(db));
if (db) sqlite3_close(db);
return rc;
}

// MARK: - Store errors -

// A function that js_functions cannot store, here on a read-only connection, is registered on the
// connection but reported as an error, because it would be gone when the database is reopened.
int test_store_errors (void) {
const char *path = "js_store_errors.sqlite";
sqlite3 *db = NULL;
remove(path);

int rc = sqlite3_open(path, &db);
if (rc != SQLITE_OK) goto abort_test;
#if JS_LOAD_EMBEDDED
rc = sqlite3_js_init(db, NULL, NULL);
#else
rc = sqlite3_enable_load_extension(db, 1);
if (rc == SQLITE_OK) rc = db_exec(db, "SELECT load_extension('./dist/js');");
#endif
if (rc != SQLITE_OK) goto abort_test;
rc = check_query(db, "SELECT js_init_table();", "0");
if (rc != SQLITE_OK) goto abort_test;
sqlite3_close(db);
db = NULL;

rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, NULL);
if (rc != SQLITE_OK) goto abort_test;
#if JS_LOAD_EMBEDDED
rc = sqlite3_js_init(db, NULL, NULL);
#else
rc = sqlite3_enable_load_extension(db, 1);
if (rc == SQLITE_OK) rc = db_exec(db, "SELECT load_extension('./dist/js');");
#endif
if (rc != SQLITE_OK) goto abort_test;
rc = check_error(db, "SELECT js_create_scalar('not_stored', '(function(args){ return 42; })');", "could not be stored");
if (rc != SQLITE_OK) goto abort_test;
rc = check_query(db, "SELECT not_stored();", "42");
if (rc != SQLITE_OK) goto abort_test;

abort_test:
if (rc != SQLITE_OK && db) printf("Error: %s\n", sqlite3_errmsg(db));
if (db) sqlite3_close(db);
remove(path);
return rc;
}

// MARK: - Memory limit -

// Creates and runs an aggregate on a new connection with a memory limit of limit_kb, if not 0,
Expand Down Expand Up @@ -1488,6 +1625,8 @@ int main (void) {
result += test_report("Result set lifetime", test_rowset_lifetime());
result += test_report("Blobs", test_blobs());
result += test_report("Numbers", test_numbers());
result += test_report("Conversion errors", test_conversion_errors());
result += test_report("Store errors", test_store_errors());
result += test_report("Memory limit", test_memory_limit());
result += test_report("Stack size", test_stack_size());
result += test_report("Stack on other threads", test_stack_threads());
Expand All @@ -1499,7 +1638,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;
}
Loading