Skip to content

Commit abb9708

Browse files
authored
mcf: avoid panic in PasswordHash::push_displayable (#2118)
It's advised for `Display` impls not to error, but in the event that they do, this bubbles up the error rather than panicking
1 parent 6863922 commit abb9708

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

mcf/src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ impl fmt::Display for Error {
1717
f.write_str("modular crypt format error")
1818
}
1919
}
20+
21+
impl From<fmt::Error> for Error {
22+
fn from(_: fmt::Error) -> Self {
23+
Error {}
24+
}
25+
}

mcf/src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,7 @@ impl<'a> TryFrom<&'a str> for &'a PasswordHashRef {
112112
#[cfg(feature = "alloc")]
113113
mod allocating {
114114
use crate::{Error, Field, PasswordHashRef, Result, fields, validate, validate_id};
115-
use alloc::{
116-
borrow::ToOwned,
117-
string::{String, ToString},
118-
};
115+
use alloc::{borrow::ToOwned, string::String};
119116
use core::{borrow::Borrow, fmt, ops::Deref, str::FromStr};
120117

121118
#[cfg(feature = "base64")]
@@ -186,7 +183,9 @@ mod allocating {
186183
/// added characters comprise a valid field.
187184
pub fn push_displayable<D: fmt::Display>(&mut self, displayable: D) -> Result<()> {
188185
// TODO(tarcieri): avoid intermediate allocation?
189-
self.push_str(&displayable.to_string())
186+
let mut buf = String::new();
187+
fmt::write(&mut buf, format_args!("{}", displayable))?;
188+
self.push_str(&buf)
190189
}
191190

192191
/// Push an additional field onto the password hash string, first adding a `$` delimiter.

0 commit comments

Comments
 (0)