From 79d417a90dcfc425733a05901c9ce325bd21a2de Mon Sep 17 00:00:00 2001 From: Charlie Gordon Date: Mon, 12 Feb 2024 18:34:52 +0100 Subject: [PATCH] Simplify and clarify URL quoting js_std_urlGet --- quickjs-libc.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/quickjs-libc.c b/quickjs-libc.c index 3a12124..0e618c0 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -1337,7 +1337,7 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValue this_val, DynBuf header_buf_s, *header_buf = &header_buf_s; char *buf; size_t i, len; - int c, status; + int status; JSValue response = JS_UNDEFINED, ret_obj; JSValue options_obj; FILE *f; @@ -1365,17 +1365,20 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValue this_val, js_std_dbuf_init(ctx, &cmd_buf); dbuf_printf(&cmd_buf, "%s '", URL_GET_PROGRAM); - len = strlen(url); - for(i = 0; i < len; i++) { - switch (c = url[i]) { + for(i = 0; url[i] != '\0'; i++) { + unsigned char c = url[i]; + switch (c) { case '\'': + /* shell single quoted string does not support \' */ dbuf_putstr(&cmd_buf, "'\\''"); break; case '[': case ']': case '{': case '}': case '\\': + /* prevent interpretation by curl as range or set specification */ dbuf_putc(&cmd_buf, '\\'); /* FALLTHROUGH */ default: dbuf_putc(&cmd_buf, c); + break; } } JS_FreeCString(ctx, url);