From f5b6e7f58a108ae2394bda4ef98650cb4a50597e Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 3 Oct 2021 00:46:10 +0000 Subject: [PATCH] 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. --- lightning/src/util/ser.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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) } -- 2.39.5