mirror of
https://github.com/lightningdevkit/rust-lightning.git
synced 2025-02-24 23:08:36 +01:00
Use TLV instead of explicit length for VecReadWrapper termination
VecReadWrapper is only used in TLVs so there is no need to prepend a length before writing/reading the objects - we can instead simply read until we reach the end of the TLV stream.
This commit is contained in:
parent
ccc828412e
commit
b385a40e4a
2 changed files with 12 additions and 9 deletions
|
@ -46,7 +46,7 @@ use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
|
||||||
pub(crate) const MAX_VALUE_MSAT: u64 = 21_000_000_0000_0000_000;
|
pub(crate) const MAX_VALUE_MSAT: u64 = 21_000_000_0000_0000_000;
|
||||||
|
|
||||||
/// An error in decoding a message or struct.
|
/// An error in decoding a message or struct.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub enum DecodeError {
|
pub enum DecodeError {
|
||||||
/// A version byte specified something we don't know how to handle.
|
/// A version byte specified something we don't know how to handle.
|
||||||
/// Includes unknown realm byte in an OnionHopData packet
|
/// Includes unknown realm byte in an OnionHopData packet
|
||||||
|
|
|
@ -249,28 +249,31 @@ impl<T: Readable> Readable for OptionDeserWrapper<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const MAX_ALLOC_SIZE: u64 = 64*1024;
|
/// Wrapper to write each element of a Vec with no length prefix
|
||||||
|
|
||||||
pub(crate) struct VecWriteWrapper<'a, T: Writeable>(pub &'a Vec<T>);
|
pub(crate) struct VecWriteWrapper<'a, T: Writeable>(pub &'a Vec<T>);
|
||||||
impl<'a, T: Writeable> Writeable for VecWriteWrapper<'a, T> {
|
impl<'a, T: Writeable> Writeable for VecWriteWrapper<'a, T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
|
||||||
(self.0.len() as u64).write(writer)?;
|
|
||||||
for ref v in self.0.iter() {
|
for ref v in self.0.iter() {
|
||||||
v.write(writer)?;
|
v.write(writer)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Wrapper to read elements from a given stream until it reaches the end of the stream.
|
||||||
pub(crate) struct VecReadWrapper<T: Readable>(pub Vec<T>);
|
pub(crate) struct VecReadWrapper<T: Readable>(pub Vec<T>);
|
||||||
impl<T: Readable> Readable for VecReadWrapper<T> {
|
impl<T: Readable> Readable for VecReadWrapper<T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
|
fn read<R: Read>(mut reader: &mut R) -> Result<Self, DecodeError> {
|
||||||
let count: u64 = Readable::read(reader)?;
|
let mut values = Vec::new();
|
||||||
let mut values = Vec::with_capacity(cmp::min(count, MAX_ALLOC_SIZE / (core::mem::size_of::<T>() as u64)) as usize);
|
loop {
|
||||||
for _ in 0..count {
|
let mut track_read = ReadTrackingReader::new(&mut reader);
|
||||||
match Readable::read(reader) {
|
match Readable::read(&mut track_read) {
|
||||||
Ok(v) => { values.push(v); },
|
Ok(v) => { values.push(v); },
|
||||||
|
// If we failed to read any bytes at all, we reached the end of our TLV
|
||||||
|
// stream and have simply exhausted all entries.
|
||||||
|
Err(ref e) if e == &DecodeError::ShortRead && !track_read.have_read => break,
|
||||||
Err(e) => return Err(e),
|
Err(e) => return Err(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue