From 23b433fe6c65044d36285562a685f2bfc075eb7f Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Fri, 20 Sep 2024 04:05:17 -0400 Subject: [PATCH] Fix #612 --- networks/monero/wallet/src/extra.rs | 3 ++- networks/monero/wallet/src/tests/extra.rs | 17 +++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/networks/monero/wallet/src/extra.rs b/networks/monero/wallet/src/extra.rs index 521d69ae..537e595a 100644 --- a/networks/monero/wallet/src/extra.rs +++ b/networks/monero/wallet/src/extra.rs @@ -302,7 +302,8 @@ impl Extra { // `fill_buf` returns the current buffer, filled if empty, only empty if the reader is // exhausted while !r.fill_buf()?.is_empty() { - res.0.push(ExtraField::read(r)?); + let Ok(field) = ExtraField::read(r) else { break }; + res.0.push(field); } Ok(res) } diff --git a/networks/monero/wallet/src/tests/extra.rs b/networks/monero/wallet/src/tests/extra.rs index 2f331e61..497602ce 100644 --- a/networks/monero/wallet/src/tests/extra.rs +++ b/networks/monero/wallet/src/tests/extra.rs @@ -8,7 +8,7 @@ use crate::{ // Tests derived from // https://github.com/monero-project/monero/blob/ac02af92867590ca80b2779a7bbeafa99ff94dcb/ // tests/unit_tests/test_tx_utils.cpp -// which is licensed +// which is licensed as follows: #[rustfmt::skip] /* Copyright (c) 2014-2022, The Monero Project @@ -105,13 +105,15 @@ fn padding_only_max_size() { #[test] fn padding_only_exceed_max_size() { let buf: Vec = vec![0; MAX_TX_EXTRA_PADDING_COUNT + 1]; - Extra::read::<&[u8]>(&mut buf.as_ref()).unwrap_err(); + let extra = Extra::read::<&[u8]>(&mut buf.as_ref()).unwrap(); + assert!(extra.0.is_empty()); } #[test] fn invalid_padding_only() { let buf: Vec = vec![0, 42]; - Extra::read::<&[u8]>(&mut buf.as_ref()).unwrap_err(); + let extra = Extra::read::<&[u8]>(&mut buf.as_ref()).unwrap(); + assert!(extra.0.is_empty()); } #[test] @@ -135,7 +137,8 @@ fn extra_nonce_only_wrong_size() { let mut buf: Vec = vec![0; 20]; buf[0] = 2; buf[1] = 255; - Extra::read::<&[u8]>(&mut buf.as_ref()).unwrap_err(); + let extra = Extra::read::<&[u8]>(&mut buf.as_ref()).unwrap(); + assert!(extra.0.is_empty()); } #[test] @@ -155,7 +158,8 @@ fn pub_key_and_padding() { fn pub_key_and_invalid_padding() { let mut buf: Vec = PUB_KEY_BYTES.to_vec(); buf.extend([0, 1]); - Extra::read::<&[u8]>(&mut buf.as_ref()).unwrap_err(); + let extra = Extra::read::<&[u8]>(&mut buf.as_ref()).unwrap(); + assert_eq!(extra.0, vec![ExtraField::PublicKey(pub_key())]); } #[test] @@ -181,7 +185,8 @@ fn extra_mysterious_minergate_only_wrong_size() { let mut buf: Vec = vec![0; 20]; buf[0] = 222; buf[1] = 255; - Extra::read::<&[u8]>(&mut buf.as_ref()).unwrap_err(); + let extra = Extra::read::<&[u8]>(&mut buf.as_ref()).unwrap(); + assert!(extra.0.is_empty()); } #[test]