From a7d417cf634459724180bfc299e49f79aca41db0 Mon Sep 17 00:00:00 2001 From: "Jeong, YunWon" Date: Sun, 28 Dec 2025 11:09:55 +0900 Subject: [PATCH] Fix socket.getattrinfo --- crates/stdlib/src/socket.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/crates/stdlib/src/socket.rs b/crates/stdlib/src/socket.rs index 08b05b56a..8af0bdd86 100644 --- a/crates/stdlib/src/socket.rs +++ b/crates/stdlib/src/socket.rs @@ -1951,13 +1951,31 @@ mod _socket { flags: opts.flags, }; - let host = opts.host.as_ref().map(|s| s.as_str()); - let port = opts.port.as_ref().map(|p| -> std::borrow::Cow<'_, str> { - match p { - Either::A(s) => s.as_str().into(), - Either::B(i) => i.to_string().into(), + // Encode host using IDNA encoding + let host_encoded: Option = match opts.host.as_ref() { + Some(s) => { + let encoded = + vm.state + .codec_registry + .encode_text(s.to_owned().into(), "idna", None, vm)?; + let host_str = std::str::from_utf8(encoded.as_bytes()) + .map_err(|_| vm.new_runtime_error("idna output is not utf8".to_owned()))?; + Some(host_str.to_owned()) } - }); + None => None, + }; + let host = host_encoded.as_deref(); + + // Encode port using UTF-8 + let port: Option> = match opts.port.as_ref() { + Some(Either::A(s)) => { + Some(std::borrow::Cow::Borrowed(s.to_str().ok_or_else(|| { + vm.new_unicode_encode_error("surrogates not allowed".to_owned()) + })?)) + } + Some(Either::B(i)) => Some(std::borrow::Cow::Owned(i.to_string())), + None => None, + }; let port = port.as_ref().map(|p| p.as_ref()); let addrs = dns_lookup::getaddrinfo(host, port, Some(hints))