From 565e1f6cdb018276528281df1dc92389f77721b7 Mon Sep 17 00:00:00 2001 From: Szilard Parrag Date: Thu, 16 Mar 2023 15:32:26 +0100 Subject: [PATCH 1/3] core-primitives/utils: fix crash if slice.len() < 2 in decode_hex() --- core-primitives/utils/src/hex.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/core-primitives/utils/src/hex.rs b/core-primitives/utils/src/hex.rs index 6f3c63e68c..c2355aad7b 100644 --- a/core-primitives/utils/src/hex.rs +++ b/core-primitives/utils/src/hex.rs @@ -57,10 +57,12 @@ pub fn hex_encode(data: &[u8]) -> String { /// Helper method for decoding hex. pub fn decode_hex>(message: T) -> Result> { - let mut message = message.as_ref(); - if message[..2] == [b'0', b'x'] { - message = &message[2..] - } + let message = message.as_ref(); + let message = match message { + [b'0', b'x', hex_value @ ..] => hex_value, + _ => &message, + }; + let decoded_message = hex::decode(message).map_err(Error::Hex)?; Ok(decoded_message) } @@ -80,6 +82,26 @@ mod tests { assert_eq!(data, decoded_data); } + #[test] + fn hex_encode_decode_works_empty_input() { + let data = String::new(); + + let hex_encoded_data = hex_encode(&data.encode()); + let decoded_data = + String::decode(&mut decode_hex(hex_encoded_data).unwrap().as_slice()).unwrap(); + + assert_eq!(data, decoded_data); + } + + #[test] + fn hex_encode_decode_works_empty_input_for_decode() { + let data = String::new(); + + let decoded_data = decode_hex(&data).unwrap(); + + assert!(decoded_data.is_empty()); + } + #[test] fn to_hex_from_hex_works() { let data = "Hello World!".to_string(); From 75a4d712b5686878660da4e9470795479c6634fe Mon Sep 17 00:00:00 2001 From: Szilard Parrag Date: Thu, 16 Mar 2023 15:54:54 +0100 Subject: [PATCH 2/3] its-consensus-common: fix unused doc comment warning --- sidechain/consensus/common/src/is_descendant_of_builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sidechain/consensus/common/src/is_descendant_of_builder.rs b/sidechain/consensus/common/src/is_descendant_of_builder.rs index a6c8edc75f..5e13c6f69a 100644 --- a/sidechain/consensus/common/src/is_descendant_of_builder.rs +++ b/sidechain/consensus/common/src/is_descendant_of_builder.rs @@ -49,7 +49,7 @@ where // If the current hash is the head and the parent is the base, then we know that // this current hash is the descendant of the parent. Otherwise we can set the // head to the parent and find the lowest common ancestor between `head` - /// and `base` in the tree. + // and `base` in the tree. if current_hash == head { if current_parent_hash == base { return Ok(true) From 3118a0e29965ac1cb2ce2cea9d6b3fbc4eac1f57 Mon Sep 17 00:00:00 2001 From: Szilard Parrag Date: Fri, 17 Mar 2023 09:14:25 +0100 Subject: [PATCH 3/3] fixup! core-primitives/utils: fix crash if slice.len() < 2 in decode_hex() --- core-primitives/utils/src/hex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-primitives/utils/src/hex.rs b/core-primitives/utils/src/hex.rs index c2355aad7b..8543cba15f 100644 --- a/core-primitives/utils/src/hex.rs +++ b/core-primitives/utils/src/hex.rs @@ -60,7 +60,7 @@ pub fn decode_hex>(message: T) -> Result> { let message = message.as_ref(); let message = match message { [b'0', b'x', hex_value @ ..] => hex_value, - _ => &message, + _ => message, }; let decoded_message = hex::decode(message).map_err(Error::Hex)?;