consensus_encoding: Deduplicate end() error path in decoders
What changed, and why it matters
This commit is a straightforward internal code cleanup. It removes duplicated error-handling code in three decoder 'end' methods and rewrites the logic using pattern matching and early returns. There is no change to what errors are produced or when they are produced; the behavior appears identical before and after.
No security action required. Review as normal refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors CompactSizeDecoder::end, ByteVecDecoder::end, and VecDecoder<T>::end to reduce duplicated UnexpectedEofError / ValueExceedsLimit construction. In the byte-vector decoders, a single missing value is computed and then one Err is returned. In CompactSizeDecoder, a match with a guard replaces map_err plus and_then. The error conditions, values, and success paths are preserved.
Changed components
consensus_encoding/src/compact_size.rsconsensus_encoding/src/decode/decoders.rsInspect captured patch +23 / −39
diff --git a/consensus_encoding/src/compact_size.rs b/consensus_encoding/src/compact_size.rs
index 3dee77e2..7a264b31 100644
--- a/consensus_encoding/src/compact_size.rs
+++ b/consensus_encoding/src/compact_size.rs
@@ -173,22 +173,13 @@ impl Decoder for CompactSizeDecoder {
let dec_value = compact_size_decode_u64(&self.buf)?;
- // This error is returned if dec_value is outside of the usize range, or
- // if it is above the given limit.
- let make_err = || {
- CompactSizeDecoderError(E::ValueExceedsLimit(LengthPrefixExceedsMaxError {
- value: dec_value,
+ match usize::try_from(dec_value) {
+ Ok(nsize) if nsize <= self.limit => Ok(nsize),
+ _ => Err(CompactSizeDecoderError(E::ValueExceedsLimit(LengthPrefixExceedsMaxError {
limit: self.limit,
- }))
- };
-
- usize::try_from(dec_value).map_err(|_| make_err()).and_then(|nsize| {
- if nsize > self.limit {
- Err(make_err())
- } else {
- Ok(nsize)
- }
- })
+ value: dec_value,
+ }))),
+ }
}
fn read_limit(&self) -> usize { compact_size_read_limit(&self.buf) }
diff --git a/consensus_encoding/src/decode/decoders.rs b/consensus_encoding/src/decode/decoders.rs
index ab9ab5b3..7d9c714e 100644
--- a/consensus_encoding/src/decode/decoders.rs
+++ b/consensus_encoding/src/decode/decoders.rs
@@ -123,19 +123,15 @@ impl Decoder for ByteVecDecoder {
use ByteVecDecoderError as E;
use ByteVecDecoderErrorInner as Inner;
- if let Some(ref prefix_decoder) = self.prefix_decoder {
- return Err(E(Inner::UnexpectedEof(UnexpectedEofError {
- missing: prefix_decoder.read_limit(),
- })));
- }
-
- if self.bytes_written == self.bytes_expected {
- Ok(self.buffer)
+ let missing = if let Some(ref prefix_decoder) = self.prefix_decoder {
+ prefix_decoder.read_limit()
+ } else if self.bytes_written != self.bytes_expected {
+ self.bytes_expected - self.bytes_written
} else {
- Err(E(Inner::UnexpectedEof(UnexpectedEofError {
- missing: self.bytes_expected - self.bytes_written,
- })))
- }
+ return Ok(self.buffer);
+ };
+
+ Err(E(Inner::UnexpectedEof(UnexpectedEofError { missing })))
}
fn read_limit(&self) -> usize {
@@ -284,19 +280,16 @@ impl<T: Decode> Decoder for VecDecoder<T> {
fn end(self) -> Result<Self::Output, Self::Error> {
use VecDecoderErrorInner as E;
- if let Some(ref prefix_decoder) = self.prefix_decoder {
- return Err(VecDecoderError(E::UnexpectedEof(UnexpectedEofError {
- missing: prefix_decoder.read_limit(),
- })));
- }
-
- if self.buffer.len() == self.length {
- Ok(self.buffer)
+ let len = self.buffer.len();
+ let missing = if let Some(prefix_decoder) = self.prefix_decoder {
+ prefix_decoder.read_limit()
+ } else if len != self.length {
+ self.length - len
} else {
- Err(VecDecoderError(E::UnexpectedEof(UnexpectedEofError {
- missing: self.length - self.buffer.len(),
- })))
- }
+ return Ok(self.buffer);
+ };
+
+ Err(VecDecoderError(E::UnexpectedEof(UnexpectedEofError { missing })))
}
fn read_limit(&self) -> usize {
Why this scored 15/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.