Skip to content

Commit

Permalink
fix(serde-utils): decode empty hex properly (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
benluelo authored Oct 17, 2023
2 parents b5b8c77 + 6f3adbb commit dbaf4ae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/serde-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

15 changes: 14 additions & 1 deletion lib/serde-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate alloc;

use std::fmt::Debug;
use core::fmt::Debug;

use hex::FromHexError;

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -335,3 +338,13 @@ pub mod bitvec_string {
})
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn hex() {
parse_hex::<Vec<u8>>(to_hex([])).unwrap();
}
}

0 comments on commit dbaf4ae

Please sign in to comment.