Skip to content

Commit 9b23c9c

Browse files
committed
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`
1 parent f982bb9 commit 9b23c9c

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ pub fn encode_upper_to_writer<Ck: Checksum, W: std::io::Write>(
345345

346346
/// An error while decoding an address.
347347
#[cfg(feature = "alloc")]
348-
#[derive(Debug)]
348+
#[derive(Debug, Clone, PartialEq, Eq)]
349+
#[non_exhaustive]
349350
pub enum DecodeError {
350351
/// Parsing failed.
351352
Parse(UncheckedHrpstringError),
@@ -379,12 +380,14 @@ impl std::error::Error for DecodeError {
379380

380381
#[cfg(feature = "alloc")]
381382
impl From<UncheckedHrpstringError> for DecodeError {
383+
#[inline]
382384
fn from(e: UncheckedHrpstringError) -> Self { Self::Parse(e) }
383385
}
384386

385387
/// An error while decoding an address from a reader.
386388
#[cfg(feature = "std")]
387389
#[derive(Debug)]
390+
#[non_exhaustive]
388391
pub enum DecodeFromReaderError {
389392
/// Read error.
390393
Read(std::io::Error),
@@ -418,11 +421,13 @@ impl std::error::Error for DecodeFromReaderError {
418421

419422
#[cfg(feature = "std")]
420423
impl From<std::io::Error> for DecodeFromReaderError {
424+
#[inline]
421425
fn from(e: std::io::Error) -> Self { Self::Read(e) }
422426
}
423427

424428
#[cfg(feature = "std")]
425429
impl From<DecodeError> for DecodeFromReaderError {
430+
#[inline]
426431
fn from(e: DecodeError) -> Self { Self::Decode(e) }
427432
}
428433

src/primitives/decode.rs

+11
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ where
545545

546546
/// An error while constructing a [`SegwitHrpstring`] type.
547547
#[derive(Debug, Clone, PartialEq, Eq)]
548+
#[non_exhaustive]
548549
pub enum SegwitHrpstringError {
549550
/// Error while parsing the encoded address string.
550551
Unchecked(UncheckedHrpstringError),
@@ -591,23 +592,28 @@ impl std::error::Error for SegwitHrpstringError {
591592
}
592593

593594
impl From<UncheckedHrpstringError> for SegwitHrpstringError {
595+
#[inline]
594596
fn from(e: UncheckedHrpstringError) -> Self { Self::Unchecked(e) }
595597
}
596598

597599
impl From<WitnessLengthError> for SegwitHrpstringError {
600+
#[inline]
598601
fn from(e: WitnessLengthError) -> Self { Self::WitnessLength(e) }
599602
}
600603

601604
impl From<PaddingError> for SegwitHrpstringError {
605+
#[inline]
602606
fn from(e: PaddingError) -> Self { Self::Padding(e) }
603607
}
604608

605609
impl From<ChecksumError> for SegwitHrpstringError {
610+
#[inline]
606611
fn from(e: ChecksumError) -> Self { Self::Checksum(e) }
607612
}
608613

609614
/// An error while constructing a [`CheckedHrpstring`] type.
610615
#[derive(Debug, Clone, PartialEq, Eq)]
616+
#[non_exhaustive]
611617
pub enum CheckedHrpstringError {
612618
/// Error while parsing the encoded address string.
613619
Parse(UncheckedHrpstringError),
@@ -639,10 +645,12 @@ impl std::error::Error for CheckedHrpstringError {
639645
}
640646

641647
impl From<UncheckedHrpstringError> for CheckedHrpstringError {
648+
#[inline]
642649
fn from(e: UncheckedHrpstringError) -> Self { Self::Parse(e) }
643650
}
644651

645652
impl From<ChecksumError> for CheckedHrpstringError {
653+
#[inline]
646654
fn from(e: ChecksumError) -> Self { Self::Checksum(e) }
647655
}
648656

@@ -680,10 +688,12 @@ impl std::error::Error for UncheckedHrpstringError {
680688
}
681689

682690
impl From<CharError> for UncheckedHrpstringError {
691+
#[inline]
683692
fn from(e: CharError) -> Self { Self::Char(e) }
684693
}
685694

686695
impl From<hrp::Error> for UncheckedHrpstringError {
696+
#[inline]
687697
fn from(e: hrp::Error) -> Self { Self::Hrp(e) }
688698
}
689699

@@ -770,6 +780,7 @@ impl std::error::Error for ChecksumError {
770780

771781
/// Error validating the padding bits on the witness data.
772782
#[derive(Debug, Clone, PartialEq, Eq)]
783+
#[non_exhaustive]
773784
pub enum PaddingError {
774785
/// The data payload has too many bits of padding.
775786
TooMuch,

src/primitives/gf32.rs

+2
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,12 @@ impl std::error::Error for Error {
361361
}
362362

363363
impl From<num::TryFromIntError> for Error {
364+
#[inline]
364365
fn from(e: num::TryFromIntError) -> Self { Error::NotAByte(e) }
365366
}
366367

367368
impl From<Infallible> for Error {
369+
#[inline]
368370
fn from(i: Infallible) -> Self { match i {} }
369371
}
370372

src/primitives/segwit.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ pub fn validate_witness_program_length(
5353
}
5454

5555
/// Field element does not represent a valid witness version.
56-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56+
#[derive(Debug, Clone, PartialEq, Eq)]
57+
#[non_exhaustive]
5758
pub struct InvalidWitnessVersionError(Fe32);
5859

5960
impl fmt::Display for InvalidWitnessVersionError {
@@ -68,7 +69,7 @@ impl std::error::Error for InvalidWitnessVersionError {
6869
}
6970

7071
/// Witness program invalid because of incorrect length.
71-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72+
#[derive(Debug, Clone, PartialEq, Eq)]
7273
#[non_exhaustive]
7374
pub enum WitnessLengthError {
7475
/// The witness data is too short.

src/segwit.rs

+9
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ pub fn encode_upper_to_writer_unchecked<W: std::io::Write>(
263263
/// An error while decoding a segwit address.
264264
#[cfg(feature = "alloc")]
265265
#[derive(Debug, Clone, PartialEq, Eq)]
266+
#[non_exhaustive]
266267
pub struct DecodeError(pub SegwitHrpstringError);
267268

268269
#[cfg(feature = "alloc")]
@@ -279,12 +280,14 @@ impl std::error::Error for DecodeError {
279280

280281
#[cfg(feature = "alloc")]
281282
impl From<SegwitHrpstringError> for DecodeError {
283+
#[inline]
282284
fn from(e: SegwitHrpstringError) -> Self { Self(e) }
283285
}
284286

285287
/// An error while decoding a segwit address from a reader.
286288
#[cfg(feature = "std")]
287289
#[derive(Debug)]
290+
#[non_exhaustive]
288291
pub enum DecodeFromReaderError {
289292
/// Read error.
290293
Read(std::io::Error),
@@ -318,16 +321,19 @@ impl std::error::Error for DecodeFromReaderError {
318321

319322
#[cfg(feature = "std")]
320323
impl From<std::io::Error> for DecodeFromReaderError {
324+
#[inline]
321325
fn from(e: std::io::Error) -> Self { Self::Read(e) }
322326
}
323327

324328
#[cfg(feature = "std")]
325329
impl From<DecodeError> for DecodeFromReaderError {
330+
#[inline]
326331
fn from(e: DecodeError) -> Self { Self::Decode(e) }
327332
}
328333

329334
/// An error while constructing a [`SegwitHrpstring`] type.
330335
#[derive(Debug, Clone, PartialEq, Eq)]
336+
#[non_exhaustive]
331337
pub enum EncodeError {
332338
/// Invalid witness version (must be 0-16 inclusive).
333339
WitnessVersion(InvalidWitnessVersionError),
@@ -363,14 +369,17 @@ impl std::error::Error for EncodeError {
363369
}
364370

365371
impl From<InvalidWitnessVersionError> for EncodeError {
372+
#[inline]
366373
fn from(e: InvalidWitnessVersionError) -> Self { Self::WitnessVersion(e) }
367374
}
368375

369376
impl From<WitnessLengthError> for EncodeError {
377+
#[inline]
370378
fn from(e: WitnessLengthError) -> Self { Self::WitnessLength(e) }
371379
}
372380

373381
impl From<fmt::Error> for EncodeError {
382+
#[inline]
374383
fn from(e: fmt::Error) -> Self { Self::Write(e) }
375384
}
376385

0 commit comments

Comments
 (0)