Skip to content

Commit f693d7e

Browse files
authored
Enforce explicit Some around raw values (#494)
1 parent ba66c8e commit f693d7e

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/ser/mod.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,20 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
842842
T: ?Sized + Serialize,
843843
{
844844
if name == crate::value::raw::RAW_VALUE_TOKEN {
845-
return guard_recursion! { self => value.serialize(raw::RawValueSerializer::new(self)) };
845+
let implicit_some_depth = self.implicit_some_depth;
846+
self.implicit_some_depth = 0;
847+
848+
for _ in 0..implicit_some_depth {
849+
self.output.write_all(b"Some(")?;
850+
}
851+
852+
guard_recursion! { self => value.serialize(raw::RawValueSerializer::new(self)) }?;
853+
854+
for _ in 0..implicit_some_depth {
855+
self.output.write_all(b")")?;
856+
}
857+
858+
return Ok(());
846859
}
847860

848861
if self.extensions().contains(Extensions::UNWRAP_NEWTYPES) || self.newtype_variant {

tests/407_raw_value.rs

+17
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,21 @@ fn test_fuzzer_found_issue() {
306306
ron::from_str::<&RawValue>("a").unwrap(),
307307
RawValue::from_ron("a").unwrap()
308308
);
309+
310+
assert_eq!(
311+
ron::to_string(&Some(Some(RawValue::from_ron("None").unwrap()))).unwrap(),
312+
"Some(Some(None))"
313+
);
314+
// Since a RawValue can contain anything, no implicit Some are allowed around it
315+
assert_eq!(
316+
ron::Options::default()
317+
.with_default_extension(ron::extensions::Extensions::IMPLICIT_SOME)
318+
.to_string(&Some(Some(RawValue::from_ron("None").unwrap())))
319+
.unwrap(),
320+
"Some(Some(None))"
321+
);
322+
assert_eq!(
323+
ron::from_str::<Option<Option<&RawValue>>>("Some(Some(None))").unwrap(),
324+
Some(Some(RawValue::from_ron("None").unwrap()))
325+
);
309326
}

0 commit comments

Comments
 (0)