From 604fa5d8fbde63e80c008e43d32be6971cf20b8e Mon Sep 17 00:00:00 2001 From: Tim Bruijnzeels Date: Wed, 12 Aug 2020 14:20:30 +0200 Subject: [PATCH] Clean up potentially unsafe unwrap() statements. --- src/commons/api/admin.rs | 23 ++++++++--------------- src/commons/eventsourcing/store.rs | 10 ++++++---- src/commons/remote/rfc6492.rs | 2 +- src/daemon/ca/server.rs | 2 +- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/commons/api/admin.rs b/src/commons/api/admin.rs index 76182b6c..d2cb58df 100644 --- a/src/commons/api/admin.rs +++ b/src/commons/api/admin.rs @@ -11,7 +11,6 @@ use serde::de; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use rpki::cert::Cert; -use rpki::crypto::Signer; use rpki::uri; use rpki::x509::Time; @@ -53,11 +52,14 @@ impl TryFrom<&PathBuf> for Handle { type Error = InvalidHandle; fn try_from(path: &PathBuf) -> Result { - let path = path.file_name().unwrap(); - let s = path.to_string_lossy().to_string(); - let s = s.replace("+", "/"); - let s = s.replace("=", "\\"); - Self::from_str(&s) + if let Some(path) = path.file_name() { + let s = path.to_string_lossy().to_string(); + let s = s.replace("+", "/"); + let s = s.replace("=", "\\"); + Self::from_str(&s) + } else { + Err(InvalidHandle) + } } } @@ -129,15 +131,6 @@ pub struct InvalidHandle; #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)] pub struct Token(String); -impl Token { - pub fn random(signer: &S) -> Self { - let mut res = <[u8; 20]>::default(); - signer.rand(&mut res).unwrap(); - let string = hex::encode(res); - Token(string) - } -} - impl From<&str> for Token { fn from(s: &str) -> Self { Token(s.to_string()) diff --git a/src/commons/eventsourcing/store.rs b/src/commons/eventsourcing/store.rs index 36f445fe..b1c3c041 100644 --- a/src/commons/eventsourcing/store.rs +++ b/src/commons/eventsourcing/store.rs @@ -375,10 +375,12 @@ impl KeyStore for DiskKeyStore { if let Ok(dir) = fs::read_dir(&self.dir) { for d in dir { - let path = d.unwrap().path(); - if path.is_dir() { - if let Ok(id) = Handle::try_from(&path) { - res.push(id); + if let Ok(d) = d { + let path = d.path(); + if path.is_dir() { + if let Ok(id) = Handle::try_from(&path) { + res.push(id); + } } } } diff --git a/src/commons/remote/rfc6492.rs b/src/commons/remote/rfc6492.rs index 8c332cea..b7530a5c 100644 --- a/src/commons/remote/rfc6492.rs +++ b/src/commons/remote/rfc6492.rs @@ -55,7 +55,7 @@ pub struct Message { /// # Data Access /// impl Message { - pub fn unwrap(self) -> (Sender, Recipient, Content) { + pub fn unpack(self) -> (Sender, Recipient, Content) { (self.sender, self.recipient, self.content) } diff --git a/src/daemon/ca/server.rs b/src/daemon/ca/server.rs index da7024db..ac6059fa 100644 --- a/src/daemon/ca/server.rs +++ b/src/daemon/ca/server.rs @@ -329,7 +329,7 @@ impl CaServer { let content = ca.verify_rfc6492(msg)?; - let (child, recipient, content) = content.unwrap(); + let (child, recipient, content) = content.unpack(); let cms_logger = CmsLogger::for_rfc6492_rcvd(self.rfc6492_log_dir.as_ref(), &recipient, &child);