From 3333dbab24b53110d6b01ed89ae39c4e2f3f34db Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 16 Jan 2024 10:27:23 +1100 Subject: [PATCH] Use new read_to_limit function In the `psbt` code we have a custom `read_to_end` function, now we have `io::Read::read_to_limit` we can remove this function. --- bitcoin/src/psbt/raw.rs | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/bitcoin/src/psbt/raw.rs b/bitcoin/src/psbt/raw.rs index ca59651a..9f9c64fd 100644 --- a/bitcoin/src/psbt/raw.rs +++ b/bitcoin/src/psbt/raw.rs @@ -8,7 +8,7 @@ use core::fmt; -use io::{BufRead, Read, Write}; +use io::{BufRead, Write}; use super::serialize::{Deserialize, Serialize}; use crate::consensus::encode::{ @@ -162,7 +162,11 @@ where fn consensus_decode(r: &mut R) -> Result { let prefix = Vec::::consensus_decode(r)?; let subtype = Subtype::from(r.read_u8()?); - let key = read_to_end(r)?; + + // The limit is a DOS protection mechanism the exact value is not + // important, 1024 bytes is bigger than any key should be. + let mut key = vec![]; + let _ = r.read_to_limit(&mut key, 1024)?; Ok(ProprietaryKey { prefix, subtype, key }) } @@ -194,17 +198,3 @@ where Ok(deserialize(&key.key)?) } } - -pub(crate) fn read_to_end(d: &mut D) -> Result, io::Error> { - let mut result = vec![]; - let mut buf = [0u8; 64]; - loop { - match d.read(&mut buf) { - Ok(0) => break, - Ok(n) => result.extend_from_slice(&buf[0..n]), - Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} - Err(e) => return Err(e), - }; - } - Ok(result) -}