Clean up potentially unsafe unwrap() statements.

This commit is contained in:
Tim Bruijnzeels
2020-08-12 14:20:30 +02:00
parent 3e22e97abc
commit 604fa5d8fb
4 changed files with 16 additions and 21 deletions
+8 -15
View File
@@ -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<Self, Self::Error> {
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<S: Signer>(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())
+6 -4
View File
@@ -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);
}
}
}
}
+1 -1
View File
@@ -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)
}
+1 -1
View File
@@ -329,7 +329,7 @@ impl<S: Signer> CaServer<S> {
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);