Skip to content
Merged
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
37 changes: 30 additions & 7 deletions templates/full/wasm_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,40 @@ static const char* get_keyword_name(PgQueryKeywordKind keyword_kind) {
}
}

static size_t scan_json_escaped_length(const char* text, int length) {
size_t escaped_length = 0;
for (int i = 0; i < length; i++) {
unsigned char c = text[i];
size_t width;
if (c == '"' || c == '\\' || c == '\n' || c == '\r' || c == '\t') {
width = 2;
} else {
width = c < 0x20 ? 6 : 1;
}
if (escaped_length > SIZE_MAX - width) {
return SIZE_MAX;
}
escaped_length += width;
}
return escaped_length;
}

static char* build_scan_json(const PgQueryScanTokensResult *scan_result, const char* original_sql) {
if (!scan_result || !original_sql) {
return safe_strdup("{\"version\":0,\"tokens\":[]}");
}

// Size the buffer from token text length, not token count: each token
// contributes at most ~170 bytes of fixed JSON plus up to 2 bytes per input
// byte once escaped.
// Reserve space for each token's escaped text and fixed JSON fields.
size_t estimated_size = 1024;
for (int i = 0; i < scan_result->n_tokens; i++) {
const PgQueryScanToken *token = &scan_result->tokens[i];
int token_length = token->end - token->start;
if (token_length < 0) token_length = 0;
size_t token_size = (size_t) token_length * 2 + 200;
size_t escaped_length = scan_json_escaped_length(&original_sql[token->start], token_length);
if (escaped_length > SIZE_MAX - 200) {
return safe_strdup("out of memory");
}
size_t token_size = escaped_length + 200;
if (estimated_size > SIZE_MAX - token_size) {
return safe_strdup("out of memory");
}
Expand All @@ -300,7 +320,7 @@ static char* build_scan_json(const PgQueryScanTokensResult *scan_result, const c
int token_length = token->end - token->start;
if (token_length < 0) token_length = 0; // Safety check

char* token_text = safe_malloc(token_length + 1);
char* token_text = safe_malloc((size_t) token_length + 1);
if (!token_text) {
free(json);
return safe_strdup("out of memory");
Expand All @@ -312,14 +332,15 @@ static char* build_scan_json(const PgQueryScanTokensResult *scan_result, const c
token_text[token_length] = '\0';

// Escape token text for JSON
char* escaped_text = safe_malloc(token_length * 2 + 1);
size_t escaped_length = scan_json_escaped_length(token_text, token_length);
char* escaped_text = safe_malloc(escaped_length + 1);
if (!escaped_text) {
free(token_text);
free(json);
return safe_strdup("out of memory");
}

int escaped_pos = 0;
size_t escaped_pos = 0;
for (int j = 0; j < token_length; j++) {
char c = token_text[j];
if (c == '"' || c == '\\') {
Expand All @@ -334,6 +355,8 @@ static char* build_scan_json(const PgQueryScanTokensResult *scan_result, const c
} else if (c == '\t') {
escaped_text[escaped_pos++] = '\\';
escaped_text[escaped_pos++] = 't';
} else if ((unsigned char) c < 0x20) {
escaped_pos += snprintf(escaped_text + escaped_pos, 7, "\\u%04x", (unsigned int) c);
} else {
escaped_text[escaped_pos++] = c;
}
Expand Down
37 changes: 30 additions & 7 deletions versions/18/src/wasm_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,20 +273,40 @@ static const char* get_keyword_name(PgQueryKeywordKind keyword_kind) {
}
}

static size_t scan_json_escaped_length(const char* text, int length) {
size_t escaped_length = 0;
for (int i = 0; i < length; i++) {
unsigned char c = text[i];
size_t width;
if (c == '"' || c == '\\' || c == '\n' || c == '\r' || c == '\t') {
width = 2;
} else {
width = c < 0x20 ? 6 : 1;
}
if (escaped_length > SIZE_MAX - width) {
return SIZE_MAX;
}
escaped_length += width;
}
return escaped_length;
}

static char* build_scan_json(const PgQueryScanTokensResult *scan_result, const char* original_sql) {
if (!scan_result || !original_sql) {
return safe_strdup("{\"version\":0,\"tokens\":[]}");
}

// Size the buffer from token text length, not token count: each token
// contributes at most ~170 bytes of fixed JSON plus up to 2 bytes per input
// byte once escaped.
// Reserve space for each token's escaped text and fixed JSON fields.
size_t estimated_size = 1024;
for (int i = 0; i < scan_result->n_tokens; i++) {
const PgQueryScanToken *token = &scan_result->tokens[i];
int token_length = token->end - token->start;
if (token_length < 0) token_length = 0;
size_t token_size = (size_t) token_length * 2 + 200;
size_t escaped_length = scan_json_escaped_length(&original_sql[token->start], token_length);
if (escaped_length > SIZE_MAX - 200) {
return safe_strdup("out of memory");
}
size_t token_size = escaped_length + 200;
if (estimated_size > SIZE_MAX - token_size) {
return safe_strdup("out of memory");
}
Expand All @@ -307,7 +327,7 @@ static char* build_scan_json(const PgQueryScanTokensResult *scan_result, const c
int token_length = token->end - token->start;
if (token_length < 0) token_length = 0; // Safety check

char* token_text = safe_malloc(token_length + 1);
char* token_text = safe_malloc((size_t) token_length + 1);
if (!token_text) {
free(json);
return safe_strdup("out of memory");
Expand All @@ -319,14 +339,15 @@ static char* build_scan_json(const PgQueryScanTokensResult *scan_result, const c
token_text[token_length] = '\0';

// Escape token text for JSON
char* escaped_text = safe_malloc(token_length * 2 + 1);
size_t escaped_length = scan_json_escaped_length(token_text, token_length);
char* escaped_text = safe_malloc(escaped_length + 1);
if (!escaped_text) {
free(token_text);
free(json);
return safe_strdup("out of memory");
}

int escaped_pos = 0;
size_t escaped_pos = 0;
for (int j = 0; j < token_length; j++) {
char c = token_text[j];
if (c == '"' || c == '\\') {
Expand All @@ -341,6 +362,8 @@ static char* build_scan_json(const PgQueryScanTokensResult *scan_result, const c
} else if (c == '\t') {
escaped_text[escaped_pos++] = '\\';
escaped_text[escaped_pos++] = 't';
} else if ((unsigned char) c < 0x20) {
escaped_pos += snprintf(escaped_text + escaped_pos, 7, "\\u%04x", (unsigned int) c);
} else {
escaped_text[escaped_pos++] = c;
}
Expand Down
11 changes: 11 additions & 0 deletions versions/18/test/scan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,5 +303,16 @@ comment */ SELECT 2`;
assert.equal(t.text, `$$${literal}$$`);
}
});

it("should preserve control characters in literals and comments in both scan APIs", async () => {
const controls = Array.from({ length: 31 }, (_, i) => String.fromCharCode(i + 1)).join("");
const literal = `$$${controls.repeat(128)}$$`;
const comment = `/* ${controls} */`;
const sql = `SELECT ${literal} ${comment};`;

for (const { tokens } of [query.scanSync(sql), await query.scan(sql)]) {
assert.deepEqual(tokens.map(t => t.text), ["SELECT", literal, comment, ";"]);
}
});
});
});
Loading