- Fix recursive `Display::fmt` implementation on `Error` to avoid stack overflow

- Added unit test to cover a sample case
This commit is contained in:
FujiApple 2020-01-31 13:11:40 +08:00
parent e89fa41b7a
commit 2f359b515c
1 changed files with 12 additions and 1 deletions

View File

@ -117,7 +117,7 @@ impl fmt::Display for Error {
| ErrorKind::ImmatureSignature
| ErrorKind::InvalidAlgorithm
| ErrorKind::InvalidKeyFormat
| ErrorKind::InvalidAlgorithmName => write!(f, "{}", self),
| ErrorKind::InvalidAlgorithmName => write!(f, "{:?}", self.0),
ErrorKind::Json(ref err) => write!(f, "JSON error: {}", err),
ErrorKind::Utf8(ref err) => write!(f, "UTF-8 error: {}", err),
ErrorKind::Crypto(ref err) => write!(f, "Crypto error: {}", err),
@ -162,3 +162,14 @@ impl From<ErrorKind> for Error {
new_error(kind)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_rendering() {
assert_eq!("InvalidAlgorithmName", Error::from(ErrorKind::InvalidAlgorithmName).to_string());
}
}