From 59f5a54d43c383bda1ca8608a89b1e77f3c75fb9 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 4 Oct 2023 15:56:45 +1100 Subject: [PATCH] Audit error types Audit all error types and ensure the following holds: - All use `non_exhaustive` - All derive `Debug, Clone, PartialEq, Eq` (unless `io::Error` is present, in which case only `Debug`) - All error `From` impls use `inline` --- src/lib.rs | 7 ++++++- src/primitives/decode.rs | 11 +++++++++++ src/primitives/gf32.rs | 2 ++ src/primitives/segwit.rs | 5 +++-- src/segwit.rs | 9 +++++++++ 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2453d3e82..83a2d2687 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -342,7 +342,8 @@ pub fn encode_upper_to_writer( /// An error while decoding an address. #[cfg(feature = "alloc")] -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] pub enum DecodeError { /// Parsing failed. Parse(UncheckedHrpstringError), @@ -376,12 +377,14 @@ impl std::error::Error for DecodeError { #[cfg(feature = "alloc")] impl From for DecodeError { + #[inline] fn from(e: UncheckedHrpstringError) -> Self { Self::Parse(e) } } /// An error while decoding an address from a reader. #[cfg(feature = "std")] #[derive(Debug)] +#[non_exhaustive] pub enum DecodeFromReaderError { /// Read error. Read(std::io::Error), @@ -415,11 +418,13 @@ impl std::error::Error for DecodeFromReaderError { #[cfg(feature = "std")] impl From for DecodeFromReaderError { + #[inline] fn from(e: std::io::Error) -> Self { Self::Read(e) } } #[cfg(feature = "std")] impl From for DecodeFromReaderError { + #[inline] fn from(e: DecodeError) -> Self { Self::Decode(e) } } diff --git a/src/primitives/decode.rs b/src/primitives/decode.rs index 462b7d809..b0c0e133a 100644 --- a/src/primitives/decode.rs +++ b/src/primitives/decode.rs @@ -545,6 +545,7 @@ where /// An error while constructing a [`SegwitHrpstring`] type. #[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] pub enum SegwitHrpstringError { /// Error while parsing the encoded address string. Unchecked(UncheckedHrpstringError), @@ -591,23 +592,28 @@ impl std::error::Error for SegwitHrpstringError { } impl From for SegwitHrpstringError { + #[inline] fn from(e: UncheckedHrpstringError) -> Self { Self::Unchecked(e) } } impl From for SegwitHrpstringError { + #[inline] fn from(e: WitnessLengthError) -> Self { Self::WitnessLength(e) } } impl From for SegwitHrpstringError { + #[inline] fn from(e: PaddingError) -> Self { Self::Padding(e) } } impl From for SegwitHrpstringError { + #[inline] fn from(e: ChecksumError) -> Self { Self::Checksum(e) } } /// An error while constructing a [`CheckedHrpstring`] type. #[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] pub enum CheckedHrpstringError { /// Error while parsing the encoded address string. Parse(UncheckedHrpstringError), @@ -639,10 +645,12 @@ impl std::error::Error for CheckedHrpstringError { } impl From for CheckedHrpstringError { + #[inline] fn from(e: UncheckedHrpstringError) -> Self { Self::Parse(e) } } impl From for CheckedHrpstringError { + #[inline] fn from(e: ChecksumError) -> Self { Self::Checksum(e) } } @@ -680,10 +688,12 @@ impl std::error::Error for UncheckedHrpstringError { } impl From for UncheckedHrpstringError { + #[inline] fn from(e: CharError) -> Self { Self::Char(e) } } impl From for UncheckedHrpstringError { + #[inline] fn from(e: hrp::Error) -> Self { Self::Hrp(e) } } @@ -770,6 +780,7 @@ impl std::error::Error for ChecksumError { /// Error validating the padding bits on the witness data. #[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] pub enum PaddingError { /// The data payload has too many bits of padding. TooMuch, diff --git a/src/primitives/gf32.rs b/src/primitives/gf32.rs index 49439298d..2b50ab55c 100644 --- a/src/primitives/gf32.rs +++ b/src/primitives/gf32.rs @@ -361,10 +361,12 @@ impl std::error::Error for Error { } impl From for Error { + #[inline] fn from(e: num::TryFromIntError) -> Self { Error::NotAByte(e) } } impl From for Error { + #[inline] fn from(i: Infallible) -> Self { match i {} } } diff --git a/src/primitives/segwit.rs b/src/primitives/segwit.rs index 79811e02a..af9ccd729 100644 --- a/src/primitives/segwit.rs +++ b/src/primitives/segwit.rs @@ -53,7 +53,8 @@ pub fn validate_witness_program_length( } /// Field element does not represent a valid witness version. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] pub struct InvalidWitnessVersionError(Fe32); impl fmt::Display for InvalidWitnessVersionError { @@ -68,7 +69,7 @@ impl std::error::Error for InvalidWitnessVersionError { } /// Witness program invalid because of incorrect length. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] #[non_exhaustive] pub enum WitnessLengthError { /// The witness data is too short. diff --git a/src/segwit.rs b/src/segwit.rs index b0ce59728..f66451737 100644 --- a/src/segwit.rs +++ b/src/segwit.rs @@ -263,6 +263,7 @@ pub fn encode_upper_to_writer_unchecked( /// An error while decoding a segwit address. #[cfg(feature = "alloc")] #[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] pub struct DecodeError(pub SegwitHrpstringError); #[cfg(feature = "alloc")] @@ -279,12 +280,14 @@ impl std::error::Error for DecodeError { #[cfg(feature = "alloc")] impl From for DecodeError { + #[inline] fn from(e: SegwitHrpstringError) -> Self { Self(e) } } /// An error while decoding a segwit address from a reader. #[cfg(feature = "std")] #[derive(Debug)] +#[non_exhaustive] pub enum DecodeFromReaderError { /// Read error. Read(std::io::Error), @@ -318,16 +321,19 @@ impl std::error::Error for DecodeFromReaderError { #[cfg(feature = "std")] impl From for DecodeFromReaderError { + #[inline] fn from(e: std::io::Error) -> Self { Self::Read(e) } } #[cfg(feature = "std")] impl From for DecodeFromReaderError { + #[inline] fn from(e: DecodeError) -> Self { Self::Decode(e) } } /// An error while constructing a [`SegwitHrpstring`] type. #[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] pub enum EncodeError { /// Invalid witness version (must be 0-16 inclusive). WitnessVersion(InvalidWitnessVersionError), @@ -363,14 +369,17 @@ impl std::error::Error for EncodeError { } impl From for EncodeError { + #[inline] fn from(e: InvalidWitnessVersionError) -> Self { Self::WitnessVersion(e) } } impl From for EncodeError { + #[inline] fn from(e: WitnessLengthError) -> Self { Self::WitnessLength(e) } } impl From for EncodeError { + #[inline] fn from(e: fmt::Error) -> Self { Self::Write(e) } }