{"record":{"id":"758c0a8fdf33058a","repo":"rust-lang/rust","slug":"failed-to-convert-address-to-socketaddr","errorCode":null,"errorMessage":"Failed to convert address to SocketAddr: {}","messagePattern":"Failed to convert address to SocketAddr: (.+?)","errorType":"exception","errorClass":"NonIpSockAddr","httpStatus":null,"severity":"error","filePath":"library/std/src/sys/net/connection/sgx.rs","lineNumber":518,"sourceCode":"impl error::Error for NonIpSockAddr {}\n\nimpl fmt::Display for NonIpSockAddr {\n    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {\n        write!(f, \"Failed to convert address to SocketAddr: {}\", self.host)\n    }\n}\n\npub struct LookupHost(!);\n\nimpl Iterator for LookupHost {\n    type Item = SocketAddr;\n    fn next(&mut self) -> Option<SocketAddr> {\n        self.0\n    }\n}\n\npub(crate) fn lookup_host_string(addr: impl Into<String>) -> io::Result<LookupHost> {\n    Err(io::Error::new(io::ErrorKind::Uncategorized, NonIpSockAddr { host: addr.into() }))\n}\n\npub fn lookup_host(host: &str, port: u16) -> io::Result<LookupHost> {\n    lookup_host_string(format!(\"{host}:{port}\"))\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n\n    #[test]\n    fn unparseable_sockaddr() {\n        let addr = \"local\";\n        let error = addr.to_socket_addrs().unwrap_err();\n        let non_ip_addr = error.downcast::<NonIpSockAddr>().unwrap();\n        assert_eq!(addr, non_ip_addr.host);\n    }\n}","sourceCodeStart":500,"sourceCodeEnd":536,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/library/std/src/sys/net/connection/sgx.rs#L500-L536","documentation":"Produced by Rust std's SGX (Intel Software Guard Extensions) network shim lookup_host_string. SGX enclaves have no network stack and no DNS resolver, so any address that is not already a literal IP address cannot be resolved to a SocketAddr. The NonIpSockAddr error wraps the offending host string; to_socket_addrs on an SGX target always fails for non-IP input.","triggerScenarios":"Calling to_socket_addrs() / TcpStream::connect / UdpSocket::bind with a hostname (e.g. \"example.com:443\") rather than a dotted-quad/IPv6 literal while running inside an SGX enclave. The call hits lookup_host_string which unconditionally returns NonIpSockAddr{ host }.","commonSituations":"Porting a networked crate into an SGX target; third-party dependency performing DNS; connecting to a service by name from within an enclave; tests run under the sgx unknown target.","solutions":["Pre-resolve the hostname outside the enclave and pass a literal IP:port string into the enclave.","Use std::net::SocketAddr / Ipv4Addr::from(...) values constructed at compile time or supplied by the host application.","Avoid APIs that call to_socket_addrs inside SGX; gate networking behind an ocalls-based host resolver.","If you must accept hostnames, parse them to an IpAddr first and only connect when parsing succeeds."],"exampleFix":"// before\nlet s = TcpStream::connect(\"example.com:443\")?;\n\n// after (inside SGX)\nlet addr: SocketAddr = ([93,184,216,34], 443).into();\nlet s = TcpStream::connect(addr)?;","handlingStrategy":"validation","validationCode":"fn is_literal_ip(host: &str) -> bool {\n    host.parse::<std::net::IpAddr>().is_ok()\n}\n// Before connecting inside SGX:\nif !is_literal_ip(host) {\n    return Err(\"SGX requires a literal IP address\".into());\n}","typeGuard":"fn literal_socket_addr(s: &str) -> Option<std::net::SocketAddr> {\n    s.parse().ok()\n}","tryCatchPattern":"match (host, port).to_socket_addrs() {\n    Ok(mut it) => Ok(it.next().unwrap()),\n    Err(e) if e.downcast_ref::<NonIpSockAddr>().is_some() => {\n        // ask host app for a pre-resolved IP\n        Err(\"hostname not resolvable inside SGX\".into())\n    }\n    Err(e) => Err(e),\n}","preventionTips":["Resolve hostnames on the host and pass SocketAddr literals into the enclave.","Parse input with IpAddr first; only connect when parsing succeeds.","Avoid third-party crates that call to_socket_addrs inside SGX."],"tags":["rust","std","network","sgx","dns","socket"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}