Merge rust-bitcoin/rust-bitcoin#809: Use write_all instead of write

22aeaef52b Use write_all instead of write (Riccardo Casatta)

Pull request description:

  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)

ACKs for top commit:
  apoelstra:
    ACK 22aeaef52b
  Kixunil:
    ACK 22aeaef52b
  dr-orlovsky:
    utACK 22aeaef52b

Tree-SHA512: e4bf3c757e1d369f9bb737e970b93ec29a487419eb478b41c36da033eafea3f0a96faa1e5c6f9397febba309ce4330b29a9e5369bb547645e5f72ba935d2cafe
This commit is contained in:
Dr. Maxim Orlovsky 2022-01-26 00:18:01 +02:00
commit 97cc70073b
No known key found for this signature in database
GPG Key ID: AF91255DEA466640
3 changed files with 9 additions and 10 deletions

View File

@ -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)
}

View File

@ -147,7 +147,8 @@ impl<Subtype> Encodable for ProprietaryKey<Subtype> where Subtype: Copy + From<u
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, io::Error> {
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)
}
}

View File

@ -615,11 +615,10 @@ impl TaprootMerkleBranch {
/// Serialize to a writer. Returns the number of bytes written
pub fn encode<Write: io::Write>(&self, mut writer: Write) -> io::Result<usize> {
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<Write: io::Write>(&self, mut writer: Write) -> io::Result<usize> {
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