Add witness decoding with oversized length test
What changed, and why it matters
This commit only adds a new unit test for an existing function. It does not change any production code, so it cannot introduce or fix a security vulnerability by itself. The test verifies that witness data with a length of exactly 4,000,000 bytes is accepted, while a length of 4,000,001 bytes is rejected.
No security action required. Treat as routine test coverage improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change is a test-only addition in primitives/src/witness.rs. It adds a decode_max_length test exercising WitnessDecoder against the boundary value in cast_to_usize_if_valid(). The first half builds a valid witness containing one 4,000,000-byte element and asserts successful decoding. The second half builds an invalid witness with a 4,000,001-byte length prefix and asserts that push_bytes returns LengthPrefixInvalid. No runtime logic is modified.
Changed components
primitives/src/witness.rs (test module only)Inspect captured patch +24 / −0
diff --git a/primitives/src/witness.rs b/primitives/src/witness.rs
index 358c4700..22c522e6 100644
--- a/primitives/src/witness.rs
+++ b/primitives/src/witness.rs
@@ -1276,4 +1276,28 @@ mod test {
assert_eq!(got, want);
}
+
+ #[cfg(feature = "alloc")]
+ #[test]
+ fn decode_max_length() {
+ let mut encoded = Vec::new();
+ encoded.extend_from_slice(compact_size::encode(1usize).as_slice());
+ encoded.extend_from_slice(compact_size::encode(4_000_000usize).as_slice());
+ encoded.resize(encoded.len() + 4_000_000, 0u8);
+
+ let mut slice = encoded.as_slice();
+ let mut decoder = WitnessDecoder::new();
+ decoder.push_bytes(&mut slice).unwrap();
+ let witness = decoder.end().unwrap();
+ assert_eq!(witness[0].len(), 4_000_000);
+
+ let mut encoded = Vec::new();
+ encoded.extend_from_slice(compact_size::encode(1usize).as_slice());
+ encoded.extend_from_slice(compact_size::encode(4_000_001usize).as_slice());
+
+ let mut slice = encoded.as_slice();
+ let mut decoder = WitnessDecoder::new();
+ let err = decoder.push_bytes(&mut slice).unwrap_err();
+ assert!(matches!(err, WitnessDecoderError(WitnessDecoderErrorInner::LengthPrefixInvalid(_))));
+ }
}
Why this scored 12/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.