From df7df72734391c2227d3a9233b647b7cdd365d88 Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Wed, 1 Nov 2023 20:46:20 +1100 Subject: [PATCH] Allow missing trailing fields to be deserialised into None 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. --- dlc-messages/src/ser_impls.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/dlc-messages/src/ser_impls.rs b/dlc-messages/src/ser_impls.rs index 52273c07..85503c08 100644 --- a/dlc-messages/src/ser_impls.rs +++ b/dlc-messages/src/ser_impls.rs @@ -453,7 +453,14 @@ pub fn read_option_cb( where F: Fn(&mut R) -> Result, { - 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)?),