From a2dd5b421b52b3904adf6408da727e530f7ebda4 Mon Sep 17 00:00:00 2001 From: h4sh3d Date: Wed, 3 Nov 2021 18:22:45 +0100 Subject: [PATCH 1/2] Improve coverage for encode_block and decode_block, fix bug --- src/base58.rs | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/base58.rs b/src/base58.rs index a112a11..6063614 100644 --- a/src/base58.rs +++ b/src/base58.rs @@ -152,7 +152,7 @@ fn u8be_to_u64(data: &[u8]) -> u64 { } fn encode_block(data: &[u8]) -> Result<[char; FULL_ENCODED_BLOCK_SIZE]> { - if data.is_empty() && data.len() > FULL_BLOCK_SIZE { + if data.is_empty() || data.len() > FULL_BLOCK_SIZE { return Err(Error::InvalidBlockSize); } let mut res = ['1'; FULL_ENCODED_BLOCK_SIZE]; @@ -174,7 +174,7 @@ struct DecodedBlock { } fn decode_block(data: &[u8]) -> Result { - if data.is_empty() && data.len() > FULL_ENCODED_BLOCK_SIZE { + if data.len() > FULL_ENCODED_BLOCK_SIZE { return Err(Error::InvalidBlockSize); } let res_size = match ENCODED_BLOCK_SIZES.iter().position(|&x| x == data.len()) { @@ -199,9 +199,7 @@ fn decode_block(data: &[u8]) -> Result { let max: u128 = match res_size { 8 => std::u64::MAX as u128 + 1, 0..=7 => 1 << (res_size * 8), - _ => { - return Err(Error::Overflow); - } + _ => unreachable!(), }; let data = if (res as u128) < max { @@ -255,9 +253,11 @@ where if len == 0 { // EOF reached, final block is created - let block_size = ENCODED_BLOCK_SIZES[clen]; - for c in &encode_block(&buf[..clen])?[..block_size] { - yield *c; + if clen > 0 { + let block_size = ENCODED_BLOCK_SIZES[clen]; + for c in &encode_block(&buf[..clen])?[..block_size] { + yield *c; + } } break; @@ -470,7 +470,7 @@ where mod tests { use super::{ decode, decode_block, encode, encode_block, u8be_to_u64, Error, ENCODED_BLOCK_SIZES, - FULL_BLOCK_SIZE, + FULL_BLOCK_SIZE, FULL_ENCODED_BLOCK_SIZE, }; #[cfg(feature = "check")] @@ -483,6 +483,32 @@ mod tests { #[cfg(feature = "stream")] use futures_util::{pin_mut, stream::StreamExt}; + #[test] + fn encode_wrong_block() { + assert_eq!(encode_block(&[0u8; 0]), Err(Error::InvalidBlockSize)); + assert_eq!( + encode_block(&[0u8; FULL_BLOCK_SIZE + 1]), + Err(Error::InvalidBlockSize) + ); + } + + #[test] + fn encode_empty_value() { + assert_eq!(encode(&[0u8; 0]), Ok(String::from(""))); + } + + #[test] + fn decode_wrong_block() { + assert_eq!(decode_block(&[0u8; 1]), Err(Error::InvalidBlockSize)); + assert_eq!(decode_block(&[0u8; 4]), Err(Error::InvalidBlockSize)); + assert_eq!(decode_block(&[0u8; 8]), Err(Error::InvalidBlockSize)); + assert_eq!( + decode_block(&[0u8; FULL_ENCODED_BLOCK_SIZE + 1]), + Err(Error::InvalidBlockSize) + ); + //assert!(false); + } + macro_rules! uint_8be_to_64 { ($expected:expr, $string:expr) => { assert_eq!($expected, u8be_to_u64($string)); From a38b7f6760fc99962dc00a4042b21249c6570e17 Mon Sep 17 00:00:00 2001 From: h4sh3d Date: Mon, 15 Nov 2021 13:14:13 +0100 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ca42a2..ce206b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Shared workflow to automate release management and publication on [crates.io](https://crates.io) ([#14](https://github.com/monero-rs/base58-monero/pull/14)) +### Fixed + +- Fix a bug in the condition that validates inputs in `encode_block` and `decode_block` ([#13](https://github.com/monero-rs/base58-monero/pull/13)) + ## [0.3.1] - 2021-09-27 ### Changed