From 65c18ddc604748a87d3c218de794c1e5358daba8 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Mon, 5 Aug 2019 18:06:24 +1000 Subject: [PATCH] Fix Bitfield from_bytes empty vec bug (#487) Credit to @kirk-baird for finding the bug with the fuzzer. Co-authored-by: Kirk Baird --- eth2/utils/ssz_types/src/bitfield.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/eth2/utils/ssz_types/src/bitfield.rs b/eth2/utils/ssz_types/src/bitfield.rs index 78182712b..592574e15 100644 --- a/eth2/utils/ssz_types/src/bitfield.rs +++ b/eth2/utils/ssz_types/src/bitfield.rs @@ -163,8 +163,7 @@ impl Bitfield> { pub fn from_bytes(bytes: Vec) -> Result { let mut initial_bitfield: Bitfield> = { let num_bits = bytes.len() * 8; - Bitfield::from_raw_bytes(bytes, num_bits) - .expect("Must have adequate bytes for bit count.") + Bitfield::from_raw_bytes(bytes, num_bits)? }; let len = initial_bitfield @@ -802,6 +801,11 @@ mod bitlist { #[test] fn ssz_decode() { + assert!(BitList0::from_ssz_bytes(&[]).is_err()); + assert!(BitList1::from_ssz_bytes(&[]).is_err()); + assert!(BitList8::from_ssz_bytes(&[]).is_err()); + assert!(BitList16::from_ssz_bytes(&[]).is_err()); + assert!(BitList0::from_ssz_bytes(&[0b0000_0000]).is_err()); assert!(BitList1::from_ssz_bytes(&[0b0000_0000, 0b0000_0000]).is_err()); assert!(BitList8::from_ssz_bytes(&[0b0000_0000]).is_err());