diff --git a/src/digests.rs b/src/digests.rs index f6b0c520..4954551f 100644 --- a/src/digests.rs +++ b/src/digests.rs @@ -321,7 +321,9 @@ impl<'a, T: TryFrom> MultihashRefGeneric<'a, T> { return Err(DecodeError::BadInputLength); } - let (_code, bytes) = varint_decode::u64(&input).map_err(|_| DecodeError::BadInputLength)?; + let (code, bytes) = varint_decode::u64(&input).map_err(|_| DecodeError::BadInputLength)?; + // Make sure it's a code that is part of the codec table + T::try_from(code).map_err(|_| DecodeError::UnknownCode)?; let (hash_len, bytes) = varint_decode::u64(&bytes).map_err(|_| DecodeError::BadInputLength)?; diff --git a/tests/lib.rs b/tests/lib.rs index 3a7ad01a..c7982c14 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -286,6 +286,14 @@ fn multihash_ref_errors() { MultihashRef::from_slice(&[identity_code, identity_length, 1, 2, 3, 4]).is_err(), "Should error on wrong hash length" ); + + let unsupported_code = 0x04; + let hash_length = 3; + assert_eq!( + MultihashRef::from_slice(&[unsupported_code, hash_length, 1, 2, 3]), + Err(DecodeError::UnknownCode), + "Should error on codes that are not part of the code table" + ); } #[test]