Skip to content

Commit

Permalink
Merge pull request #986 from birdistheword96/main
Browse files Browse the repository at this point in the history
Prevent Panic in DNS Socket by Truncating Server List
  • Loading branch information
Dirbaio authored Sep 12, 2024
2 parents 8025752 + 4739cc7 commit 8602c01
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/socket/dns.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::cmp::min;
#[cfg(feature = "async")]
use core::task::Waker;

Expand Down Expand Up @@ -149,27 +150,30 @@ pub struct Socket<'a> {
impl<'a> Socket<'a> {
/// Create a DNS socket.
///
/// # Panics
///
/// Panics if `servers.len() > MAX_SERVER_COUNT`
/// Truncates the server list if `servers.len() > MAX_SERVER_COUNT`
pub fn new<Q>(servers: &[IpAddress], queries: Q) -> Socket<'a>
where
Q: Into<ManagedSlice<'a, Option<DnsQuery>>>,
{
let truncated_servers = &servers[..min(servers.len(), DNS_MAX_SERVER_COUNT)];

Socket {
servers: Vec::from_slice(servers).unwrap(),
servers: Vec::from_slice(truncated_servers).unwrap(),
queries: queries.into(),
hop_limit: None,
}
}

/// Update the list of DNS servers, will replace all existing servers
///
/// # Panics
///
/// Panics if `servers.len() > MAX_SERVER_COUNT`
/// Truncates the server list if `servers.len() > MAX_SERVER_COUNT`
pub fn update_servers(&mut self, servers: &[IpAddress]) {
self.servers = Vec::from_slice(servers).unwrap();
if servers.len() > DNS_MAX_SERVER_COUNT {
net_trace!("Max DNS Servers exceeded. Increase MAX_SERVER_COUNT");
self.servers = Vec::from_slice(&servers[..DNS_MAX_SERVER_COUNT]).unwrap();
} else {
self.servers = Vec::from_slice(servers).unwrap();
}
}

/// Return the time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
Expand Down

0 comments on commit 8602c01

Please sign in to comment.