Skip to content

Commit

Permalink
Allow missing trailing fields to be deserialised into None
Browse files Browse the repository at this point in the history
Before this patch we would fail to deserialise a struct with a
trailing optional field if the field was completely absent from the
serialised version. We only supported serialised fields that were
explicitly set to `None` by being tagged with a 0 prefix.
  • Loading branch information
luckysori committed Dec 1, 2023
1 parent 745717d commit df7df72
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion dlc-messages/src/ser_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,14 @@ pub fn read_option_cb<R: ::std::io::Read, T, F>(
where
F: Fn(&mut R) -> Result<T, DecodeError>,
{
let prefix: u8 = Readable::read(reader)?;
let prefix: u8 = match Readable::read(reader) {
Ok(prefix) => prefix,
// If there is nothing else to read, the optional field could just be missing, which is
// valid.
Err(DecodeError::ShortRead) => return Ok(None),
Err(e) => return Err(e),
};

let res = match prefix {
0 => None,
1 => Some(cb(reader)?),
Expand Down

0 comments on commit df7df72

Please sign in to comment.