From 2473149eff9e560e721b3a74642da2240f059d4b Mon Sep 17 00:00:00 2001 From: "Sebastian R. Verschoor" Date: Fri, 15 Jan 2021 16:27:58 -0500 Subject: [PATCH] Allow empty buffers for value zero in methods --- src/biguint.rs | 8 ++++++++ tests/biguint.rs | 2 ++ 2 files changed, 10 insertions(+) diff --git a/src/biguint.rs b/src/biguint.rs index 432ff849..71778cb5 100644 --- a/src/biguint.rs +++ b/src/biguint.rs @@ -2423,6 +2423,10 @@ impl BigUint { "The radix must be within 2...256" ); + if buf.is_empty() { + return Some(Zero::zero()); + } + if radix != 256 && buf.iter().any(|&b| b >= radix as u8) { return None; } @@ -2466,6 +2470,10 @@ impl BigUint { "The radix must be within 2...256" ); + if buf.is_empty() { + return Some(Zero::zero()); + } + if radix != 256 && buf.iter().any(|&b| b >= radix as u8) { return None; } diff --git a/tests/biguint.rs b/tests/biguint.rs index cc274954..3752c61d 100644 --- a/tests/biguint.rs +++ b/tests/biguint.rs @@ -1551,6 +1551,8 @@ fn test_from_and_to_radix() { } assert!(BigUint::from_radix_le(&[10, 100, 10], 50).is_none()); + assert_eq!(BigUint::from_radix_le(&[], 2), Some(BigUint::zero())); + assert_eq!(BigUint::from_radix_be(&[], 2), Some(BigUint::zero())); } #[test]