From 22aeaef52bbeeb66a5968a945ca3a45841cee6da Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Tue, 25 Jan 2022 14:50:06 +0100 Subject: [PATCH] Use write_all instead of write write() could write only a part of the given buffer, the caller should check the numbers of byte written (which is what write_all does) --- src/util/bip32.rs | 2 +- src/util/psbt/raw.rs | 3 ++- src/util/taproot.rs | 14 ++++++-------- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/util/bip32.rs b/src/util/bip32.rs index 9e13a9d0..f218fec3 100644 --- a/src/util/bip32.rs +++ b/src/util/bip32.rs @@ -770,7 +770,7 @@ impl ExtendedPubKey { /// Returns the HASH160 of the chaincode pub fn identifier(&self) -> XpubIdentifier { let mut engine = XpubIdentifier::engine(); - engine.write(&self.public_key.serialize()).expect("engines don't error"); + engine.write_all(&self.public_key.serialize()).expect("engines don't error"); XpubIdentifier::from_engine(engine) } diff --git a/src/util/psbt/raw.rs b/src/util/psbt/raw.rs index 3f9a8563..bc11a976 100644 --- a/src/util/psbt/raw.rs +++ b/src/util/psbt/raw.rs @@ -147,7 +147,8 @@ impl Encodable for ProprietaryKey where Subtype: Copy + From(&self, mut e: W) -> Result { let mut len = self.prefix.consensus_encode(&mut e)? + 1; e.emit_u8(self.subtype.into())?; - len += e.write(&self.key)?; + e.write_all(&self.key)?; + len += self.key.len(); Ok(len) } } diff --git a/src/util/taproot.rs b/src/util/taproot.rs index a42666af..487cf437 100644 --- a/src/util/taproot.rs +++ b/src/util/taproot.rs @@ -615,11 +615,10 @@ impl TaprootMerkleBranch { /// Serialize to a writer. Returns the number of bytes written pub fn encode(&self, mut writer: Write) -> io::Result { - let mut written = 0; for hash in self.0.iter() { - written += writer.write(hash)?; + writer.write_all(hash)?; } - Ok(written) + Ok(self.0.len() * sha256::Hash::LEN) } /// Serialize self as bytes @@ -704,11 +703,10 @@ impl ControlBlock { /// Serialize to a writer. Returns the number of bytes written pub fn encode(&self, mut writer: Write) -> io::Result { let first_byte: u8 = i32::from(self.output_key_parity) as u8 | self.leaf_version.to_consensus(); - let mut bytes_written = 0; - bytes_written += writer.write(&[first_byte])?; - bytes_written += writer.write(&self.internal_key.serialize())?; - bytes_written += self.merkle_branch.encode(&mut writer)?; - Ok(bytes_written) + writer.write_all(&[first_byte])?; + writer.write_all(&self.internal_key.serialize())?; + self.merkle_branch.encode(&mut writer)?; + Ok(self.size()) } /// Serialize the control block. This would be required when