Skip to content
Closed
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
12 changes: 8 additions & 4 deletions templates/full/wasm_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions versions/18/src/wasm_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions versions/18/test/scan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
Loading