From 6f3adbb2d0f2269eb3ad81a8af557e90e59774a3 Mon Sep 17 00:00:00 2001 From: benluelo Date: Tue, 17 Oct 2023 12:07:32 +0100 Subject: [PATCH] fix(serde-utils): decode empty hex properly --- lib/serde-utils/Cargo.toml | 6 +++++- lib/serde-utils/src/lib.rs | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/serde-utils/Cargo.toml b/lib/serde-utils/Cargo.toml index 1e3ce1f848..535b1c9e1e 100644 --- a/lib/serde-utils/Cargo.toml +++ b/lib/serde-utils/Cargo.toml @@ -7,6 +7,10 @@ edition = "2021" [dependencies] base64 = { version = "0.21", default-features = false, features = ["alloc"] } serde = { version = "1.0", default-features = false } -hex = { version = "0.4.3", default-features = false } +hex = { version = "0.4.3", default-features = false, features = ["alloc"] } primitive-types = { version = "0.12.1", default-features = false, features = ["serde_no_std"] } bitvec = "1.0.1" + +[dev-dependencies] +hex = { version = "0.4.3" } + diff --git a/lib/serde-utils/src/lib.rs b/lib/serde-utils/src/lib.rs index 9a6f51c140..912ebf092d 100644 --- a/lib/serde-utils/src/lib.rs +++ b/lib/serde-utils/src/lib.rs @@ -1,6 +1,6 @@ extern crate alloc; -use std::fmt::Debug; +use core::fmt::Debug; use hex::FromHexError; @@ -66,6 +66,9 @@ where } match s.strip_prefix(HEX_ENCODING_PREFIX.as_bytes()) { + Some([b'0']) => { + T::try_from(vec![]).map_err(|err| FromHexStringError::TryFromBytes(format!("{err:?}"))) + } Some(maybe_hex) => hex::decode(maybe_hex) .map_err(FromHexStringError::Hex)? .try_into() @@ -335,3 +338,13 @@ pub mod bitvec_string { }) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn hex() { + parse_hex::>(to_hex([])).unwrap(); + } +}