From fdbc5beed39609067d46958fd4d44a7a977caf0b Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 26 Jun 2026 09:34:01 +0200 Subject: [PATCH] urlapi: make two #defines instead of magic values/strings in code Closes #22181 --- lib/urlapi.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/urlapi.c b/lib/urlapi.c index 77414bd2f8..07817c780c 100644 --- a/lib/urlapi.c +++ b/lib/urlapi.c @@ -53,6 +53,10 @@ /* scheme is not URL encoded, the longest libcurl supported ones are... */ #define MAX_SCHEME_LEN 40 +#define MAX_ZONEID_LEN 16 + +/* characters not allowed in hostnames */ +#define HOSTNAME_INVALID_CHARS " \r\n\t/:#?!@{}[]\\$\'\"^`*<>=;,+&()%|" /* * If USE_IPV6 is disabled, we still want to parse IPv6 addresses, so make @@ -425,13 +429,13 @@ UNITTEST CURLUcode ipv6_parse(struct Curl_URL *u, char *hostname, hlen = len; if(hostname[len] == '%') { /* this could now be '%[zone id]' */ - char zoneid[16]; + char zoneid[MAX_ZONEID_LEN]; int i = 0; char *h = &hostname[len + 1]; /* pass '25' if present and is a URL encoded percent sign */ if(!strncmp(h, "25", 2) && h[2] && (h[2] != ']')) h += 2; - while(*h && (*h != ']') && (i < 15)) + while(*h && (*h != ']') && (i < (MAX_ZONEID_LEN - 1))) zoneid[i++] = *h++; if(!i || (']' != *h)) return CURLUE_BAD_IPV6; @@ -474,7 +478,7 @@ static CURLUcode hostname_check(struct Curl_URL *u, char *hostname, return ipv6_parse(u, hostname, hlen); else { /* letters from the second string are not ok */ - len = strcspn(hostname, " \r\n\t/:#?!@{}[]\\$\'\"^`*<>=;,+&()%|"); + len = strcspn(hostname, HOSTNAME_INVALID_CHARS); if(hlen != len) /* hostname with bad content */ return CURLUE_BAD_HOSTNAME;