Do not execute the default_value expr until we need it in TLV deser 2022-06-ffs-dumb-ser
authorMatt Corallo <git@bluematt.me>
Fri, 1 Jul 2022 21:14:19 +0000 (21:14 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 5 Jul 2022 17:32:21 +0000 (17:32 +0000)
commitf1b9bd34b83f9e8161b6d7bbc56a420cc572502a
treea7c53b99191974bdcaf0b516ce6a340c62297ba1
parentd246e613793e2a1ec016f75027ec6ef2e80f3418
Do not execute the default_value expr until we need it in TLV deser

This fixes an insta-panic in `ChannelMonitor` deserialization where
we always `unwrap` a previous value to determine the default value
of a later field. However, because we always ran the `unwrap`
before the previous field is read, we'd always panic.

The fix is rather simple - use a `OptionDeserWrapper` for
`default_value` fields and only fill in the default value if no
value was read while walking the TLV stream.

The only complexity comes from our desire to support
`read_tlv_field` calls that use an explicit field rather than an
`Option` of some sort, which requires some statement which can
assign both an `OptionDeserWrapper<T>` variable and a `T` variable.
We settle on `x = t.into()` and implement `From<T> for
OptionDeserWrapper<T>` which works, though it requires users to
specify types explicitly due to Rust determining expression types
prior to macro execution, completely guessing with no knowlege for
integer expressions (see
https://github.com/rust-lang/rust/issues/91369).
lightning/src/ln/channelmanager.rs
lightning/src/util/config.rs
lightning/src/util/ser.rs
lightning/src/util/ser_macros.rs