diff --git a/templates/full/wasm_wrapper.c b/templates/full/wasm_wrapper.c index 268d289..f2547e2 100644 --- a/templates/full/wasm_wrapper.c +++ b/templates/full/wasm_wrapper.c @@ -283,7 +283,7 @@ static char* build_scan_json(PgQuery__ScanResult *scan_result, const char* origi } char* json = safe_malloc(estimated_size); if (!json) { - return safe_strdup("{\"version\":0,\"tokens\":[]}"); + return NULL; } // Start building JSON @@ -297,7 +297,10 @@ static char* build_scan_json(PgQuery__ScanResult *scan_result, const char* origi if (token_length < 0) token_length = 0; // Safety check char* token_text = safe_malloc(token_length + 1); - if (!token_text) continue; + if (!token_text) { + free(json); + return NULL; + } if (token_length > 0) { strncpy(token_text, &original_sql[token->start], token_length); @@ -308,7 +311,8 @@ static char* build_scan_json(PgQuery__ScanResult *scan_result, const char* origi char* escaped_text = safe_malloc(token_length * 2 + 1); if (!escaped_text) { free(token_text); - continue; + free(json); + return NULL; } int escaped_pos = 0; @@ -393,7 +397,7 @@ char* wasm_scan(const char* input) { pg_query__scan_result__free_unpacked(scan_result, NULL); pg_query_free_scan_result(result); - return json_result ? json_result : safe_strdup("{\"version\":0,\"tokens\":[]}"); + return json_result ? json_result : safe_strdup("ERROR: out of memory building the scan result"); } EMSCRIPTEN_KEEPALIVE diff --git a/versions/18/src/wasm_wrapper.c b/versions/18/src/wasm_wrapper.c index 63ae5da..72ccdba 100644 --- a/versions/18/src/wasm_wrapper.c +++ b/versions/18/src/wasm_wrapper.c @@ -290,7 +290,7 @@ static char* build_scan_json(PgQuery__ScanResult *scan_result, const char* origi } char* json = safe_malloc(estimated_size); if (!json) { - return safe_strdup("{\"version\":0,\"tokens\":[]}"); + return NULL; } // Start building JSON @@ -304,7 +304,10 @@ static char* build_scan_json(PgQuery__ScanResult *scan_result, const char* origi if (token_length < 0) token_length = 0; // Safety check char* token_text = safe_malloc(token_length + 1); - if (!token_text) continue; + if (!token_text) { + free(json); + return NULL; + } if (token_length > 0) { strncpy(token_text, &original_sql[token->start], token_length); @@ -315,7 +318,8 @@ static char* build_scan_json(PgQuery__ScanResult *scan_result, const char* origi char* escaped_text = safe_malloc(token_length * 2 + 1); if (!escaped_text) { free(token_text); - continue; + free(json); + return NULL; } int escaped_pos = 0; @@ -400,7 +404,7 @@ char* wasm_scan(const char* input) { pg_query__scan_result__free_unpacked(scan_result, NULL); pg_query_free_scan_result(result); - return json_result ? json_result : safe_strdup("{\"version\":0,\"tokens\":[]}"); + return json_result ? json_result : safe_strdup("ERROR: out of memory building the scan result"); } EMSCRIPTEN_KEEPALIVE diff --git a/versions/18/test/scan.test.js b/versions/18/test/scan.test.js index bd92787..f0ace14 100644 --- a/versions/18/test/scan.test.js +++ b/versions/18/test/scan.test.js @@ -303,5 +303,11 @@ comment */ SELECT 2`; assert.equal(t.text, `$$${literal}$$`); } }); + + it("throws instead of returning no tokens when the result does not fit in memory", () => { + const sql = "SELECT 1;\n".repeat(2000000); + assert.throws(() => query.scanSync(sql), /out of memory/); + assert.equal(query.scanSync("SELECT 1").tokens.length, 2); + }); }); });