Skip to content

Commit dbefaef

Browse files
authored
Merge pull request #142 from jeandudey/2018-08-decimal-fromstr
Implement `FromStr` for `UDecimal`/`Decimal`.
2 parents a61ad5d + 9cdc75a commit dbefaef

File tree

4 files changed

+321
-58
lines changed

4 files changed

+321
-58
lines changed

fuzz/Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ honggfuzz_fuzz = ["honggfuzz"]
1414
[dependencies]
1515
honggfuzz = { version = "0.5", optional = true }
1616
afl = { version = "0.3", optional = true }
17-
bitcoin = { path = "..", features = ["fuzztarget"] }
17+
bitcoin = { path = "..", features = ["fuzztarget", "serde-decimal"] }
1818

1919
# Prevent this from interfering with workspaces
2020
[workspace]
@@ -35,3 +35,11 @@ path = "fuzz_targets/deserialize_transaction.rs"
3535
[[bin]]
3636
name = "deserialize_address"
3737
path = "fuzz_targets/deserialize_address.rs"
38+
39+
[[bin]]
40+
name = "deserialize_decimal"
41+
path = "fuzz_targets/deserialize_decimal.rs"
42+
43+
[[bin]]
44+
name = "deserialize_udecimal"
45+
path = "fuzz_targets/deserialize_udecimal.rs"
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
extern crate bitcoin;
2+
use std::str::FromStr;
3+
fn do_test(data: &[u8]) {
4+
let data_str = String::from_utf8_lossy(data);
5+
let dec = match bitcoin::util::decimal::Decimal::from_str(&data_str) {
6+
Ok(dec) => dec,
7+
Err(_) => return,
8+
};
9+
let dec_roundtrip = match bitcoin::util::decimal::Decimal::from_str(&dec.to_string()) {
10+
Ok(dec) => dec,
11+
Err(_) => return,
12+
};
13+
assert_eq!(dec, dec_roundtrip);
14+
}
15+
16+
#[cfg(feature = "afl")]
17+
extern crate afl;
18+
#[cfg(feature = "afl")]
19+
fn main() {
20+
afl::read_stdio_bytes(|data| {
21+
do_test(&data);
22+
});
23+
}
24+
25+
#[cfg(feature = "honggfuzz")]
26+
#[macro_use] extern crate honggfuzz;
27+
#[cfg(feature = "honggfuzz")]
28+
fn main() {
29+
loop {
30+
fuzz!(|data| {
31+
do_test(data);
32+
});
33+
}
34+
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
39+
let mut b = 0;
40+
for (idx, c) in hex.as_bytes().iter().enumerate() {
41+
b <<= 4;
42+
match *c {
43+
b'A'...b'F' => b |= c - b'A' + 10,
44+
b'a'...b'f' => b |= c - b'a' + 10,
45+
b'0'...b'9' => b |= c - b'0',
46+
_ => panic!("Bad hex"),
47+
}
48+
if (idx & 1) == 1 {
49+
out.push(b);
50+
b = 0;
51+
}
52+
}
53+
}
54+
55+
#[test]
56+
fn duplicate_crash() {
57+
let mut a = Vec::new();
58+
extend_vec_from_hex("00000000", &mut a);
59+
super::do_test(&a);
60+
}
61+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
extern crate bitcoin;
2+
use std::str::FromStr;
3+
fn do_test(data: &[u8]) {
4+
let data_str = String::from_utf8_lossy(data);
5+
let dec = match bitcoin::util::decimal::UDecimal::from_str(&data_str) {
6+
Ok(dec) => dec,
7+
Err(_) => return,
8+
};
9+
let dec_roundtrip = match bitcoin::util::decimal::UDecimal::from_str(&dec.to_string()) {
10+
Ok(dec) => dec,
11+
Err(_) => return,
12+
};
13+
assert_eq!(dec, dec_roundtrip);
14+
}
15+
16+
#[cfg(feature = "afl")]
17+
extern crate afl;
18+
#[cfg(feature = "afl")]
19+
fn main() {
20+
afl::read_stdio_bytes(|data| {
21+
do_test(&data);
22+
});
23+
}
24+
25+
#[cfg(feature = "honggfuzz")]
26+
#[macro_use] extern crate honggfuzz;
27+
#[cfg(feature = "honggfuzz")]
28+
fn main() {
29+
loop {
30+
fuzz!(|data| {
31+
do_test(data);
32+
});
33+
}
34+
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
39+
let mut b = 0;
40+
for (idx, c) in hex.as_bytes().iter().enumerate() {
41+
b <<= 4;
42+
match *c {
43+
b'A'...b'F' => b |= c - b'A' + 10,
44+
b'a'...b'f' => b |= c - b'a' + 10,
45+
b'0'...b'9' => b |= c - b'0',
46+
_ => panic!("Bad hex"),
47+
}
48+
if (idx & 1) == 1 {
49+
out.push(b);
50+
b = 0;
51+
}
52+
}
53+
}
54+
55+
#[test]
56+
fn duplicate_crash() {
57+
let mut a = Vec::new();
58+
extend_vec_from_hex("00000000", &mut a);
59+
super::do_test(&a);
60+
}
61+
}

0 commit comments

Comments
 (0)