socks: add assertion for hostname length in SOCKS5 connect

socks5_req0_init() rejects hostnames longer than 255 bytes, but the
later cast to unsigned char in socks5_req1_init() has no local
indication that it is safe. Add a DEBUGASSERT and comment to document
the invariant and guard against future refactoring.

Closes #21157
This commit is contained in:
Kaixuan Li 2026-03-30 19:12:55 +08:00 committed by Daniel Stenberg
parent fb6925c243
commit 2c26cea5ec
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2

View File

@ -808,6 +808,10 @@ static CURLproxycode socks5_req1_init(struct socks_state *sx,
}
else {
const size_t hostname_len = strlen(sx->hostname);
/* socks5_req0_init() already rejects hostnames longer than 255 bytes, so
this cast to unsigned char is safe. Assert to guard against future
refactoring that might remove or reorder that earlier check. */
DEBUGASSERT(hostname_len <= 255);
desttype = 3;
destination = (const unsigned char *)sx->hostname;
destlen = (unsigned char)hostname_len; /* one byte length */