From: Stefan Eissing Date: Thu, 20 Nov 2025 10:28:41 +0000 (+0100) Subject: httpsrr/altsvc: eliminate size_t casts X-Git-Tag: rc-8_18_0-1~216 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ad9b12d4114819b2c0704b89ae48c20033f05f7a;p=thirdparty%2Fcurl.git httpsrr/altsvc: eliminate size_t casts Treat alpn raw data as unsigned chars, avoids size_t and char* casts. Add method to convert a struct Curl_str to an alpnid. Closes #19621 --- diff --git a/lib/altsvc.c b/lib/altsvc.c index 357d3bc209..c05dff04e2 100644 --- a/lib/altsvc.c +++ b/lib/altsvc.c @@ -136,10 +136,8 @@ static struct altsvc *altsvc_create(struct Curl_str *srchost, size_t srcport, size_t dstport) { - enum alpnid dstalpnid = - Curl_alpn2alpnid(curlx_str(dstalpn), curlx_strlen(dstalpn)); - enum alpnid srcalpnid = - Curl_alpn2alpnid(curlx_str(srcalpn), curlx_strlen(srcalpn)); + enum alpnid dstalpnid = Curl_str2alpnid(dstalpn); + enum alpnid srcalpnid = Curl_str2alpnid(srcalpn); if(!srcalpnid || !dstalpnid) return NULL; return altsvc_createid(curlx_str(srchost), curlx_strlen(srchost), @@ -545,8 +543,7 @@ CURLcode Curl_altsvc_parse(struct Curl_easy *data, do { if(!curlx_str_single(&p, '=')) { /* [protocol]="[host][:port], [protocol]="[host][:port]" */ - enum alpnid dstalpnid = - Curl_alpn2alpnid(curlx_str(&alpn), curlx_strlen(&alpn)); + enum alpnid dstalpnid = Curl_str2alpnid(&alpn); if(!curlx_str_single(&p, '\"')) { struct Curl_str dsthost; curl_off_t port = 0; diff --git a/lib/connect.c b/lib/connect.c index b5de9b4f5e..4f9453907b 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -66,6 +66,7 @@ #include "sockaddr.h" /* required for Curl_sockaddr_storage */ #include "curlx/inet_ntop.h" #include "curlx/inet_pton.h" +#include "curlx/strparse.h" #include "vtls/vtls.h" /* for vtsl cfilters */ #include "progress.h" #include "curlx/warnless.h" @@ -81,23 +82,29 @@ #if !defined(CURL_DISABLE_ALTSVC) || defined(USE_HTTPSRR) -enum alpnid Curl_alpn2alpnid(const char *name, size_t len) +enum alpnid Curl_alpn2alpnid(const unsigned char *name, size_t len) { if(len == 2) { - if(curl_strnequal(name, "h1", 2)) + if(!memcmp(name, "h1", 2)) return ALPN_h1; - if(curl_strnequal(name, "h2", 2)) + if(!memcmp(name, "h2", 2)) return ALPN_h2; - if(curl_strnequal(name, "h3", 2)) + if(!memcmp(name, "h3", 2)) return ALPN_h3; } else if(len == 8) { - if(curl_strnequal(name, "http/1.1", 8)) + if(!memcmp(name, "http/1.1", 8)) return ALPN_h1; } return ALPN_none; /* unknown, probably rubbish input */ } +enum alpnid Curl_str2alpnid(const struct Curl_str *cstr) +{ + return Curl_alpn2alpnid((const unsigned char *)curlx_str(cstr), + curlx_strlen(cstr)); +} + #endif /* diff --git a/lib/connect.h b/lib/connect.h index ebf756c562..b6d9b8e836 100644 --- a/lib/connect.h +++ b/lib/connect.h @@ -31,8 +31,10 @@ struct Curl_dns_entry; struct ip_quadruple; +struct Curl_str; -enum alpnid Curl_alpn2alpnid(const char *name, size_t len); +enum alpnid Curl_alpn2alpnid(const unsigned char *name, size_t len); +enum alpnid Curl_str2alpnid(const struct Curl_str *str); /* generic function that returns how much time there is left to run, according to the timeouts set */ diff --git a/lib/httpsrr.c b/lib/httpsrr.c index c0392d16c4..0cf5b131bd 100644 --- a/lib/httpsrr.c +++ b/lib/httpsrr.c @@ -37,7 +37,7 @@ #include "curl_memory.h" #include "memdebug.h" -static CURLcode httpsrr_decode_alpn(const char *cp, size_t len, +static CURLcode httpsrr_decode_alpn(const uint8_t *cp, size_t len, unsigned char *alpns) { /* @@ -49,7 +49,7 @@ static CURLcode httpsrr_decode_alpn(const char *cp, size_t len, int idnum = 0; while(len > 0) { - size_t tlen = (size_t) *cp++; + size_t tlen = *cp++; enum alpnid id; len--; if(tlen > len) @@ -84,7 +84,7 @@ CURLcode Curl_httpsrr_set(struct Curl_easy *data, CURL_TRC_DNS(data, "HTTPS RR MANDATORY left to implement"); break; case HTTPS_RR_CODE_ALPN: /* str_list */ - result = httpsrr_decode_alpn((const char *)val, vlen, hi->alpns); + result = httpsrr_decode_alpn(val, vlen, hi->alpns); CURL_TRC_DNS(data, "HTTPS RR ALPN: %u %u %u %u", hi->alpns[0], hi->alpns[1], hi->alpns[2], hi->alpns[3]); break;