Skip to content

Commit

Permalink
Merge rust-bitcoin#2834: pow: Fix off-by-one error
Browse files Browse the repository at this point in the history
3298c0c pow: Unit test from_hex_internal (Tobin C. Harding)
47e4bff pow: Fix off-by-one error (Tobin C. Harding)

Pull request description:

  Patch 1 adds the fix, patch 2 is a unit test that fails if move to the front.

ACKs for top commit:
  apoelstra:
    ACK 3298c0c nice find! and lucky this just returns an error rather than panicking. may be worth backporting nonetheless
  brunoerg:
    ACK 3298c0c

Tree-SHA512: 15bbd5aa4ac62c91492f5394444d179d95770bae822422bf00bb62896dcaf6c92d32f7d2e3380c352ff7242422ec6af6ff637ff78d14406cd047ef4ad5f22649
  • Loading branch information
apoelstra committed Jun 5, 2024
2 parents 741589c + 3298c0c commit 2cfe0e2
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion bitcoin/src/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ impl U256 {

// Caller to ensure `s` does not contain a prefix.
fn from_hex_internal(s: &str) -> Result<Self, ParseIntError> {
let (high, low) = if s.len() < 32 {
let (high, low) = if s.len() <= 32 {
let low = parse::hex_u128_unchecked(s)?;
(0, low)
} else {
Expand Down Expand Up @@ -1652,6 +1652,14 @@ mod tests {
assert_eq!(got, val);
}

#[test]
fn u256_from_hex_32_characters_long() {
let hex = "a69b455cd41bb662a69b4555deadbeef";
let want = U256(0x00, 0xA69B_455C_D41B_B662_A69B_4555_DEAD_BEEF);
let got = U256::from_unprefixed_hex(hex).expect("failed to parse hex");
assert_eq!(got, want);
}

#[cfg(feature = "serde")]
#[test]
fn u256_serde() {
Expand Down

0 comments on commit 2cfe0e2

Please sign in to comment.