Commit b118ef85 authored by Marc-André Lureau's avatar Marc-André Lureau
Browse files

helper: remove panic!


Signed-off-by: default avatarMarc-André Lureau <marcandre.lureau@redhat.com>
parent fda0c8d3
......@@ -89,11 +89,12 @@ fn slirp_state_read<'a, R: Read>(
reader.read(&mut buf)?;
let in_version = i32::from_be_bytes(buf);
if in_version > libslirp::state_version() {
panic!(
return Err(format!(
"Incompatible migration data version: {} > {}",
in_version,
libslirp::state_version()
);
)
.into());
}
slirp.ctxt.state_read(in_version, reader)?;
......@@ -152,7 +153,7 @@ fn main() -> Result<(), Box<dyn Error>> {
if let Some(url) = &opt.dhcp_nbp {
let url = url::Url::parse(url)?;
if url.scheme() != "tftp" {
panic!("Invalid NBP URL");
return Err("Invalid NBP URL".into());
}
opt.slirp.tftp.name = Some(url.host_str().unwrap().to_string());
opt.slirp.tftp.bootfile = Some(url.path().to_string());
......@@ -176,7 +177,7 @@ fn main() -> Result<(), Box<dyn Error>> {
interface: Some(tun),
..
} => tun::open(tun)?,
_ => panic!("Missing a socket argument"),
_ => return Err("Missing a socket argument".into()),
};
if let Some(netns) = main_netns {
......@@ -192,7 +193,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut dbus_fd = -1;
let mut dbus = if let Some(dbus_addr) = opt.dbus_address {
if opt.dbus_id.is_none() {
panic!("You must specify an id with DBus");
return Err("You must specify an id with DBus".into());
}
let mut c = dbus::channel::Channel::open_private(&dbus_addr)?;
......@@ -295,7 +296,7 @@ fn main() -> Result<(), Box<dyn Error>> {
}
if opt.dbus_incoming && opt.incoming_fd.is_some() {
panic!("Invalid multiple incoming paths.")
return Err("Invalid multiple incoming paths.".into());
}
let mut events = Events::with_capacity(1024);
......
......@@ -24,24 +24,25 @@ pub struct InterfaceRequest {
}
impl InterfaceRequest {
pub fn with_interface_name(name: &str) -> Self {
pub fn with_interface_name(name: &str) -> Result<Self, Box<dyn Error>> {
let mut interface_request: Self = Default::default();
interface_request.set_interface_name(name);
interface_request
interface_request.set_interface_name(name)?;
Ok(interface_request)
}
pub fn set_interface_name(&mut self, name: &str) {
pub fn set_interface_name(&mut self, name: &str) -> Result<(), Box<dyn Error>> {
let name_len = name.len();
let mut name = Vec::from(name);
if name_len < INTERFACE_NAME_SIZE {
name.resize(INTERFACE_NAME_SIZE, 0);
} else {
panic!("interface name too long");
return Err("interface name too long".into());
}
assert_eq!(name.len(), INTERFACE_NAME_SIZE);
self.interface_name.clone_from_slice(&name);
Ok(())
}
}
......@@ -65,7 +66,7 @@ pub fn open(name: &str) -> Result<UnixDatagram, Box<dyn Error>> {
let flags = IFF_TAP | IFF_NO_PI;
let fd = nix::fcntl::open("/dev/net/tun", OFlag::O_RDWR, Mode::empty())?;
let mut ifr = InterfaceRequest::with_interface_name(name);
let mut ifr = InterfaceRequest::with_interface_name(name)?;
ifr.union.flags = flags;
unsafe { tun_set_iff(fd, &mut ifr as *mut InterfaceRequest as *mut i32) }?;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment