Skip to content

Commit e67134d

Browse files
Add input validation to remaining functions and fix safe_strdup
- Add input validation to wasm_deparse_protobuf, wasm_parse_query_protobuf, and wasm_get_protobuf_len - Fix safe_strdup to return NULL instead of attempting cascading allocations - Ensures consistent error handling across all WASM wrapper functions - All 32 tests continue to pass after changes Co-Authored-By: Dan Lynch <[email protected]>
1 parent d70394b commit e67134d

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/wasm_wrapper.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static char* safe_strdup(const char* str) {
1313
if (!str) return NULL;
1414
char* result = strdup(str);
1515
if (!result) {
16-
return strdup("Memory allocation failed");
16+
return NULL;
1717
}
1818
return result;
1919
}
@@ -47,6 +47,10 @@ char* wasm_parse_query(const char* input) {
4747

4848
EMSCRIPTEN_KEEPALIVE
4949
char* wasm_deparse_protobuf(const char* protobuf_data, size_t data_len) {
50+
if (!protobuf_data || data_len == 0) {
51+
return safe_strdup("Invalid input: protobuf data cannot be null or empty");
52+
}
53+
5054
PgQueryProtobuf pbuf;
5155
pbuf.data = (char*)protobuf_data;
5256
pbuf.len = data_len;
@@ -125,6 +129,11 @@ char* wasm_fingerprint(const char* input) {
125129

126130
EMSCRIPTEN_KEEPALIVE
127131
char* wasm_parse_query_protobuf(const char* input, int* out_len) {
132+
if (!validate_input(input)) {
133+
*out_len = 0;
134+
return safe_strdup("Invalid input: query cannot be null or empty");
135+
}
136+
128137
PgQueryProtobufParseResult result = pg_query_parse_protobuf(input);
129138

130139
if (result.error) {
@@ -149,6 +158,10 @@ char* wasm_parse_query_protobuf(const char* input, int* out_len) {
149158

150159
EMSCRIPTEN_KEEPALIVE
151160
int wasm_get_protobuf_len(const char* input) {
161+
if (!validate_input(input)) {
162+
return 0;
163+
}
164+
152165
PgQueryProtobufParseResult result = pg_query_parse_protobuf(input);
153166

154167
if (result.error) {

0 commit comments

Comments
 (0)