consensus_encoding: Add tests for CompactSizeDecoder limit handling
What changed, and why it matters
This commit only adds new unit tests for an existing feature. It does not change any production code, fix a bug, or alter behavior. The tests verify that a decoder correctly accepts values at or below its configured limit and rejects values above it. There is no security issue here.
No action required. This is a test-only change and does not affect security posture.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds two test functions in consensus_encoding/src/decode/decoders.rs: compact_size_new_values_too_large and compact_size_new_with_limit_values_too_large. These exercise CompactSizeDecoder::new and CompactSizeDecoder::new_with_limit boundary conditions. No implementation code is modified. The existing constructors already enforce limits; this change merely increases test coverage.
Changed components
consensus_encoding/src/decode/decoders.rsInspect captured patch +52 / −1
diff --git a/consensus_encoding/src/decode/decoders.rs b/consensus_encoding/src/decode/decoders.rs
index dd30dd5a..cb2402bf 100644
--- a/consensus_encoding/src/decode/decoders.rs
+++ b/consensus_encoding/src/decode/decoders.rs
@@ -1027,9 +1027,60 @@ mod tests {
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
- #[cfg(feature = "alloc")]
use super::*;
+ #[test]
+ fn compact_size_new_values_too_large() {
+ use CompactSizeDecoderErrorInner as E;
+
+ const EXCESS_VEC_SIZE: u64 = (MAX_VEC_SIZE + 1) as u64; // can't use try_from for const
+
+ // MAX_VEC_SIZE should succeed for `new` constructor
+ let mut decoder = CompactSizeDecoder::new();
+ decoder.push_bytes(&mut [0xFE, 0x00, 0x09, 0x3D, 0x00].as_slice()).unwrap();
+ let got = decoder.end().unwrap();
+ assert_eq!(got, MAX_VEC_SIZE);
+
+ // MAX_VEC_SIZE + 1 should fail for `new` constructor
+ let mut decoder = CompactSizeDecoder::new();
+ decoder.push_bytes(&mut [0xFE, 0x01, 0x09, 0x3D, 0x00].as_slice()).unwrap();
+ let got = decoder.end().unwrap_err();
+ assert!(matches!(
+ got,
+ CompactSizeDecoderError(E::ValueExceedsLimit(
+ LengthPrefixExceedsMaxError {
+ limit: MAX_VEC_SIZE,
+ value: EXCESS_VEC_SIZE,
+ }
+ )),
+ ));
+ }
+
+ #[test]
+ fn compact_size_new_with_limit_values_too_large() {
+ use CompactSizeDecoderErrorInner as E;
+
+ // 240 should succeed for `new_with_limit` constructor
+ let mut decoder = CompactSizeDecoder::new_with_limit(240);
+ decoder.push_bytes(&mut [0xf0].as_slice()).unwrap();
+ let got = decoder.end().unwrap();
+ assert_eq!(got, 240);
+
+ // 241 should fail for `new_with_limit` constructor
+ let mut decoder = CompactSizeDecoder::new_with_limit(240);
+ decoder.push_bytes(&mut [0xf1].as_slice()).unwrap();
+ let got = decoder.end().unwrap_err();
+ assert!(matches!(
+ got,
+ CompactSizeDecoderError(E::ValueExceedsLimit(
+ LengthPrefixExceedsMaxError {
+ limit: 240,
+ value: 241,
+ }
+ )),
+ ));
+ }
+
#[test]
#[cfg(feature = "alloc")]
fn byte_vec_decoder_decode_empty_slice() {
Why this scored 14/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.