urlapi: simplify junkscan

Makes it smaller and possibly somewhat faster

Closes #16307
This commit is contained in:
Daniel Stenberg 2025-02-12 15:59:16 +01:00
parent 597ee915c4
commit 44deccf907
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2

View File

@ -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;