consensus_encoding: add custom limit construcotrs to generic decoders
What changed, and why it matters
This commit adds new ways to create two existing decoder objects with a user-chosen size limit instead of the built-in 4 MB limit. The old default behavior is unchanged. There is no bug fix or security patch here; it is a small API convenience addition.
No security action required. Treat as a routine API enhancement. If reviewing downstream usage, verify that any calls to `new_with_limit` use an appropriate limit for their context.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces new_with_limit(limit: usize) constructors for ByteVecDecoder and VecDecoder<T> in consensus_encoding/src/decode/decoders.rs. The existing new() methods are refactored to delegate to new_with_limit(MAX_VEC_SIZE), preserving the previous 4,000,000 byte/element cap. No limits are removed or weakened by default; callers must explicitly opt into a different limit.
Changed components
consensus_encoding/src/decode/decoders.rsByteVecDecoderVecDecoder<T>Inspect captured patch +12 / −6
diff --git a/consensus_encoding/src/decode/decoders.rs b/consensus_encoding/src/decode/decoders.rs
index 8aa581c8..a9647d05 100644
--- a/consensus_encoding/src/decode/decoders.rs
+++ b/consensus_encoding/src/decode/decoders.rs
@@ -43,10 +43,13 @@ pub struct ByteVecDecoder {
#[cfg(feature = "alloc")]
impl ByteVecDecoder {
- /// Constructs a new byte decoder.
- pub const fn new() -> Self {
+ /// Constructs a new byte decoder with the default limit of 4,000,000 bytes.
+ pub const fn new() -> Self { Self::new_with_limit(MAX_VEC_SIZE) }
+
+ /// Constructs a new byte decoder with a custom limit of bytes.
+ pub const fn new_with_limit(limit: usize) -> Self {
Self {
- prefix_decoder: Some(CompactSizeDecoder::new_with_limit(MAX_VEC_SIZE)),
+ prefix_decoder: Some(CompactSizeDecoder::new_with_limit(limit)),
buffer: Vec::new(),
bytes_expected: 0,
bytes_written: 0,
@@ -185,10 +188,13 @@ where
#[cfg(feature = "alloc")]
impl<T: Decodable> VecDecoder<T> {
- /// Constructs a new byte decoder.
- pub const fn new() -> Self {
+ /// Constructs a new typed vector decoder with the default limit of 4,000,000 elements.
+ pub const fn new() -> Self { Self::new_with_limit(MAX_VEC_SIZE) }
+
+ /// Constructs a new typed vector decoder with a custom limit of elements.
+ pub const fn new_with_limit(limit: usize) -> Self {
Self {
- prefix_decoder: Some(CompactSizeDecoder::new_with_limit(MAX_VEC_SIZE)),
+ prefix_decoder: Some(CompactSizeDecoder::new_with_limit(limit)),
length: 0,
buffer: Vec::new(),
decoder: None,
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.