Skip to content

Commit 49a7662

Browse files
committed
Don't allow container map keys
1 parent 22acfdc commit 49a7662

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/serializer.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,9 +744,16 @@ impl<'a, 'w, W: io::Write> SerializeMap for &'a mut SeHeader<'w, W> {
744744
}
745745

746746
self.wtr.check_map_key(key)?;
747+
self.state = HeaderState::InStructField;
748+
// Check that the key is a scalar value.
749+
key.serialize(&mut **self)?; // This does not actually serialize anything.
750+
if let HeaderState::ErrorIfWrite(err) =
751+
mem::replace(&mut self.state, HeaderState::InStructField)
752+
{
753+
return Err(err);
754+
}
747755
let mut key_serializer = SeRecord { wtr: self.wtr };
748756
key.serialize(&mut key_serializer)?;
749-
self.state = HeaderState::InStructField;
750757
Ok(())
751758
}
752759

@@ -1154,6 +1161,24 @@ mod tests {
11541161
assert_eq!(got, "a,b");
11551162
}
11561163

1164+
#[test]
1165+
fn ordered_map_with_collection_as_key() {
1166+
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
1167+
struct MyKey {
1168+
name: &'static str,
1169+
other_attribute: u8,
1170+
}
1171+
1172+
let mut map = BTreeMap::new();
1173+
map.insert(MyKey { name: "a", other_attribute: 1 }, 2.0);
1174+
1175+
let error = serialize_header_err(map);
1176+
assert!(
1177+
matches!(error.kind(), ErrorKind::Serialize(_)),
1178+
"Expected ErrorKind::Serialize but got '{error}'"
1179+
);
1180+
}
1181+
11571182
#[test]
11581183
fn unordered_map() {
11591184
let mut writer = Writer::from_writer(vec![]);

0 commit comments

Comments
 (0)