Fix getaddrinfo hang due to infinite loop

This commit is contained in:
Wildan M
2026-01-22 16:48:13 +07:00
parent ba99b7940c
commit 6a455159ae
3 changed files with 15 additions and 25 deletions
+6 -17
View File
@@ -26,26 +26,14 @@ use super::{
sys::get_dns_server,
};
pub struct LookupHost(IntoIter<in_addr>);
impl Iterator for LookupHost {
type Item = in_addr;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl From<u32> for LookupHost {
/// from ipv4 address
fn from(s_addr: u32) -> Self {
LookupHost(vec![in_addr { s_addr }].into_iter())
}
}
pub type LookupHost = Vec<in_addr>;
pub fn lookup_host(host: &str) -> Result<LookupHost, c_int> {
if let Some(host_direct_addr) = parse_ipv4_string(host) {
// already an ip address
return Ok(host_direct_addr.into());
return Ok(vec![in_addr {
s_addr: host_direct_addr,
}]);
}
let dns_string = get_dns_server().map_err(|e| e.0)?;
@@ -134,7 +122,8 @@ pub fn lookup_host(host: &str) -> Result<LookupHost, c_int> {
}
})
.collect();
Ok(LookupHost(addrs.into_iter()))
Ok(addrs)
}
Err(_err) => Err(EINVAL),
}
+8 -7
View File
@@ -379,7 +379,7 @@ pub unsafe extern "C" fn gethostbyname(name: *const c_char) -> *mut hostent {
return ptr::null_mut();
}
};
let host_addr = match host.next() {
let host_addr = match host.into_iter().next() {
Some(result) => result,
None => {
H_ERRNO.set(HOST_NOT_FOUND);
@@ -875,7 +875,7 @@ pub unsafe extern "C" fn getaddrinfo(
let lookuphost = if ai_flags & AI_NUMERICHOST > 0 {
match parse_ipv4_string(unsafe { str::from_utf8_unchecked(node.to_bytes()) }) {
Some(s_addr) => s_addr.into(),
Some(s_addr) => vec![in_addr { s_addr }],
None => {
return EAI_NONAME;
}
@@ -923,12 +923,13 @@ pub unsafe extern "C" fn getaddrinfo(
ai_addr,
ai_next: ptr::null_mut(),
});
let mut indirect = res;
while !(unsafe { *indirect }).is_null() {
indirect = &mut unsafe { (**indirect).ai_next };
unsafe {
let mut indirect = res;
while !(*indirect).is_null() {
indirect = &mut (**indirect).ai_next;
}
*indirect = Box::into_raw(addrinfo)
}
unsafe { *indirect = Box::into_raw(addrinfo) };
}
0
+1 -1
View File
@@ -78,7 +78,7 @@ int main() {
test_timeout_case();
// TODO: "rlct_clone not implemented for aarch64 yet"
#if defined(__linux__) && defined(__aarch64__)
printf("test_success_case skipped")
printf("test_success_case skipped");
#else
test_success_case();
#endif