Skip to content

Commit 1e16c32

Browse files
committed
variable_nat_decode() check for overflow / add test case
1 parent ba2a7ea commit 1e16c32

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

rust/src/address.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ use ed25519_bip32::XPub;
66
// returns (Number represented, bytes read) if valid encoding
77
// or None if decoding prematurely finished
88
fn variable_nat_decode(bytes: &[u8]) -> Option<(u64, usize)> {
9-
let mut output = 0u64;
9+
let mut output = 0u128;
1010
let mut bytes_read = 0;
1111
for byte in bytes {
12-
output = (output << 7) | (byte & 0x7F) as u64;
12+
output = (output << 7) | (byte & 0x7F) as u128;
13+
if output > u64::MAX.into() {
14+
return None;
15+
}
1316
bytes_read += 1;
1417
if (byte & 0x80) == 0 {
15-
return Some((output, bytes_read));
18+
return Some((output as u64, bytes_read));
1619
}
1720
}
1821
None
@@ -695,6 +698,12 @@ mod tests {
695698
}
696699
}
697700

701+
#[test]
702+
fn variable_nat_decode_too_big() {
703+
let too_big = [129, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127];
704+
assert_eq!(None, variable_nat_decode(&too_big));
705+
}
706+
698707
#[test]
699708
fn base_serialize_consistency() {
700709
let base = BaseAddress::new(

0 commit comments

Comments
 (0)