Merge branch 'unwraps-in-dns' into 'master'

Handle unwraps in get_dns_server()

See merge request redox-os/relibc!655
This commit is contained in:
Jeremy Soller
2025-05-02 22:15:06 -06:00
3 changed files with 22 additions and 24 deletions
+8 -17
View File
@@ -1,29 +1,20 @@
use crate::{
c_str::CString,
error::Errno,
fs::File,
header::fcntl,
header::{errno, fcntl},
io::{BufRead, BufReader},
};
use alloc::string::String;
pub fn get_dns_server() -> String {
let file = match File::open(c"/etc/resolv.conf".into(), fcntl::O_RDONLY) {
Ok(file) => file,
Err(_) => return String::new(), // TODO: better error handling
};
let file = BufReader::new(file);
pub fn get_dns_server() -> Result<String, Errno> {
let file = File::open(c"/etc/resolv.conf".into(), fcntl::O_RDONLY).map(BufReader::new)?;
for line in file.split(b'\n') {
let mut line = match line {
Ok(line) => line,
Err(_) => return String::new(), // TODO: pls handle errors
};
if line.starts_with(b"nameserver ") {
line.drain(..11);
return String::from_utf8(line).unwrap_or_default();
for line in file.lines().map_while(Result::ok) {
if let Some(dns) = line.strip_prefix("nameserver ") {
return Ok(dns.into());
}
}
// TODO: better error handling
String::new()
Err(Errno(errno::EIO).sync())
}
+2 -2
View File
@@ -33,7 +33,7 @@ impl Iterator for LookupHost {
}
pub fn lookup_host(host: &str) -> Result<LookupHost, c_int> {
let dns_string = get_dns_server();
let dns_string = get_dns_server().map_err(|e| e.0)?;
let dns_vec: Vec<u8> = dns_string
.trim()
@@ -138,7 +138,7 @@ pub fn lookup_host(host: &str) -> Result<LookupHost, c_int> {
}
pub fn lookup_addr(addr: in_addr) -> Result<Vec<Vec<u8>>, c_int> {
let dns_string = get_dns_server();
let dns_string = get_dns_server().map_err(|e| e.0)?;
let dns_vec: Vec<u8> = dns_string
.trim()
+12 -5
View File
@@ -1,9 +1,16 @@
use crate::{fs::File, header::fcntl, io::Read};
use crate::{
error::Errno,
fs::File,
header::{errno, fcntl},
io::Read,
};
use alloc::string::String;
pub fn get_dns_server() -> String {
pub fn get_dns_server() -> Result<String, Errno> {
let mut string = String::new();
let mut file = File::open(c"/etc/net/dns".into(), fcntl::O_RDONLY).unwrap(); // TODO: error handling
file.read_to_string(&mut string).unwrap(); // TODO: error handling
string
let mut file = File::open(c"/etc/net/dns".into(), fcntl::O_RDONLY)?;
file.read_to_string(&mut string)
.map_err(|_| Errno(errno::EIO).sync())?;
Ok(string)
}