Merge PR 'Propagate read errors instead of panicking during deserialization' (#5155)
What changed, and why it matters
This change fixes three spots where the code would crash (panic) if reading data failed during deserialization. Now these spots return a proper error instead, which is safer and allows the caller to handle the problem gracefully. The included test confirms one of the fixes works for a fake failing reader.
Treat as a routine hardening fix. No immediate incident response is indicated, but downstream users should update to avoid denial-of-service scenarios caused by malformed or truncated inputs triggering panics.
Security signals we found
Replaces unwrap() with ? to prevent panics on I/O errors during deserialization
Adds regression test for error propagation in FixedLengthReader
Touches TLV decoding macro used broadly in message deserialization
Changes error handling in gossip routing deserialization path
Evidence from the diff
The commit replaces .unwrap() calls on copy(reader, &mut sink()) with ? propagation in MaybeReadable::read for NodeAnnouncementInfoDeserWrapper, FixedLengthReader::eat_remaining, and the _decode_tlv! macro. Previously, an I/O error during the discard/consume step would cause a panic. After the change, the error is propagated as a DecodeError::Io. A unit test verifies FixedLengthReader::eat_remaining returns DecodeError::Io when the underlying reader errors.
Changed components
lightning/src/util/ser.rs (FixedLengthReader::eat_remaining)lightning/src/util/ser_macros.rs (_decode_tlv! macro)lightning/src/routing/gossip.rs (NodeAnnouncementInfoDeserWrapper)Inspect captured patch +20 / −4
### lightning/src/routing/gossip.rs
@@ -1598,7 +1598,7 @@ impl MaybeReadable for NodeAnnouncementInfoDeserWrapper {
match crate::util::ser::Readable::read(reader) {
Ok(node_announcement_info) => return Ok(Some(Self(node_announcement_info))),
Err(_) => {
- copy(reader, &mut sink()).unwrap();
+ copy(reader, &mut sink())?;
return Ok(None);
},
};
### lightning/src/util/ser.rs
@@ -140,7 +140,7 @@ impl<'a, R: Read> FixedLengthReader<'a, R> {
/// Consumes the remaining bytes.
#[inline]
pub fn eat_remaining(&mut self) -> Result<(), DecodeError> {
- copy(self, &mut sink()).unwrap();
+ copy(self, &mut sink())?;
if self.bytes_read != self.total_bytes {
Err(DecodeError::ShortRead)
} else {
@@ -1813,7 +1813,7 @@ impl Readable for ClaimId {
#[cfg(test)]
mod tests {
use crate::prelude::*;
- use crate::util::ser::{Hostname, Readable, Writeable};
+ use crate::util::ser::{FixedLengthReader, Hostname, Readable, Writeable};
use bitcoin::hex::FromHex;
use bitcoin::secp256k1::ecdsa;
@@ -1901,4 +1901,20 @@ mod tests {
}
}
}
+
+ #[test]
+ fn fixed_length_reader_eat_remaining_propagates_io_errors() {
+ struct ErroringReader;
+ impl crate::io::Read for ErroringReader {
+ fn read(&mut self, _: &mut [u8]) -> Result<usize, crate::io::Error> {
+ Err(crate::io::Error::new(crate::io::ErrorKind::Other, "reader failure"))
+ }
+ }
+
+ let mut reader = ErroringReader;
+ assert_eq!(
+ FixedLengthReader::new(&mut reader, 1).eat_remaining(),
+ Err(crate::ln::msgs::DecodeError::Io(crate::io::ErrorKind::Other))
+ );
+ }
}
### lightning/src/util/ser_macros.rs
@@ -529,7 +529,7 @@ macro_rules! _decode_tlv {
// Thus, we consume everything left in the `$outer_reader` here, ensuring that if
// we're being read as a part of another TLV stream we don't spuriously fail to
// deserialize the outer object due to a TLV length mismatch.
- $crate::io_extras::copy($outer_reader, &mut $crate::io_extras::sink()).unwrap();
+ $crate::io_extras::copy($outer_reader, &mut $crate::io_extras::sink())?;
return Ok(None)
},
};Why this scored 47/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.