Skip to content

Commit dbf7df3

Browse files
committed
apply this fix to Vec<T> as well
1 parent dfedc10 commit dbf7df3

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

mls-rs-codec/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub enum Error {
5151
#[cfg_attr(feature = "std", error("Expected UTF-8 string"))]
5252
Utf8,
5353
#[cfg_attr(feature = "std", error("Invalid map content"))]
54-
InvalidMapContent,
54+
InvalidContent,
5555
#[cfg_attr(feature = "std", error("mls codec error: {0}"))]
5656
Custom(u8),
5757
}

mls-rs-codec/src/map.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ where
4848
let value = V::mls_decode(data)?;
4949

5050
if data.len() == before || items.insert(key, value).is_some() {
51-
return Err(crate::Error::InvalidMapContent);
51+
return Err(crate::Error::InvalidContent);
5252
}
5353
}
5454

@@ -92,7 +92,7 @@ where
9292
let value = V::mls_decode(data)?;
9393

9494
if data.len() == before || items.insert(key, value).is_some() {
95-
return Err(crate::Error::InvalidMapContent);
95+
return Err(crate::Error::InvalidContent);
9696
}
9797
}
9898

@@ -164,7 +164,7 @@ mod tests {
164164
#[test]
165165
fn hashmap_zero_length_structure() {
166166
let res = HashMap::<[u8; 0], [u8; 0]>::mls_decode(&mut &[0x01, 0xff][..]);
167-
assert_matches!(res, Err(crate::Error::InvalidMapContent))
167+
assert_matches!(res, Err(crate::Error::InvalidContent))
168168
}
169169

170170
#[cfg(feature = "std")]
@@ -177,7 +177,7 @@ mod tests {
177177
.unwrap();
178178

179179
let res = HashMap::<u8, u8>::mls_decode(&mut &*encoded);
180-
assert_matches!(res, Err(crate::Error::InvalidMapContent))
180+
assert_matches!(res, Err(crate::Error::InvalidContent))
181181
}
182182

183183
#[test]
@@ -189,13 +189,13 @@ mod tests {
189189
.unwrap();
190190

191191
let res = BTreeMap::<u8, u8>::mls_decode(&mut &*encoded);
192-
assert_matches!(res, Err(crate::Error::InvalidMapContent))
192+
assert_matches!(res, Err(crate::Error::InvalidContent))
193193
}
194194

195195
#[test]
196196
fn btree_map_zero_length_structure() {
197197
let res = BTreeMap::<[u8; 0], [u8; 0]>::mls_decode(&mut &[0x01, 0xff][..]);
198-
assert_matches!(res, Err(crate::Error::InvalidMapContent))
198+
assert_matches!(res, Err(crate::Error::InvalidContent))
199199
}
200200

201201
#[cfg(feature = "std")]

mls-rs-codec/src/vec.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ where
5353
let mut items = Vec::new();
5454

5555
while !data.is_empty() {
56-
items.push(T::mls_decode(data)?);
56+
let before = data.len();
57+
let value = T::mls_decode(data)?;
58+
59+
if data.len() == before {
60+
return Err(crate::Error::InvalidContent);
61+
}
62+
63+
items.push(value);
5764
}
5865

5966
Ok(items)
@@ -97,4 +104,10 @@ mod tests {
97104
Err(Error::UnexpectedEOF)
98105
);
99106
}
107+
108+
#[test]
109+
fn vec_zero_length_structure() {
110+
let res = Vec::<[u8; 0]>::mls_decode(&mut &[0x01, 0xff][..]);
111+
assert_matches!(res, Err(crate::Error::InvalidContent))
112+
}
100113
}

0 commit comments

Comments
 (0)