From: Daniel Stenberg Date: Wed, 12 Feb 2025 14:59:16 +0000 (+0100) Subject: urlapi: simplify junkscan X-Git-Tag: curl-8_13_0~490 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=44deccf9074f567654a76c1adc52c7216936c021;p=thirdparty%2Fcurl.git urlapi: simplify junkscan Makes it smaller and possibly somewhat faster Closes #16307 --- diff --git a/lib/urlapi.c b/lib/urlapi.c index 2368bd79b5..c93c9722eb 100644 --- a/lib/urlapi.c +++ b/lib/urlapi.c @@ -332,27 +332,20 @@ static CURLUcode redirect_url(char *base, const char *relurl, } /* scan for byte values <= 31, 127 and sometimes space */ -static CURLUcode junkscan(const char *url, size_t *urllen, unsigned int flags) +static CURLUcode junkscan(const char *url, size_t *urllen, bool allowspace) { - static const char badbytes[]={ - /* */ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, - 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, - 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, - 0x7f, 0x00 /* null-terminate */ - }; size_t n = strlen(url); - size_t nfine; - + size_t i; + unsigned char control; + const unsigned char *p = (const unsigned char *)url; if(n > CURL_MAX_INPUT_LENGTH) - /* excessive input length */ - return CURLUE_MALFORMED_INPUT; - - nfine = strcspn(url, badbytes); - if((nfine != n) || - (!(flags & CURLU_ALLOW_SPACE) && strchr(url, ' '))) return CURLUE_MALFORMED_INPUT; + control = allowspace ? 0x1f : 0x20; + for(i = 0; i < n; i++) { + if(p[i] <= control || p[i] == 127) + return CURLUE_MALFORMED_INPUT; + } *urllen = n; return CURLUE_OK; } @@ -946,7 +939,7 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags) Curl_dyn_init(&host, CURL_MAX_INPUT_LENGTH); - result = junkscan(url, &urllen, flags); + result = junkscan(url, &urllen, !!(flags & CURLU_ALLOW_SPACE)); if(result) goto fail;