From: Matt Corallo Date: Sun, 3 Oct 2021 00:46:10 +0000 (+0000) Subject: Implement `HashMap` read for `MaybeReadable` values X-Git-Tag: v0.0.103~11^2~8 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=f5b6e7f58a108ae2394bda4ef98650cb4a50597e;p=rust-lightning Implement `HashMap` read for `MaybeReadable` values This allows us to read a `HashMap` that has values which may be skipped if they are some backwards-compatibility type. We also take this opportunity to fail deserialization if keys are duplicated. --- diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index 47bdf04f8..ba8a9fd0d 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -502,14 +502,20 @@ impl Writeable for HashMap impl Readable for HashMap where K: Readable + Eq + Hash, - V: Readable + V: MaybeReadable { #[inline] fn read(r: &mut R) -> Result { let len: u16 = Readable::read(r)?; let mut ret = HashMap::with_capacity(len as usize); for _ in 0..len { - ret.insert(K::read(r)?, V::read(r)?); + let k = K::read(r)?; + let v_opt = V::read(r)?; + if let Some(v) = v_opt { + if ret.insert(k, v).is_some() { + return Err(DecodeError::InvalidValue); + } + } } Ok(ret) }