Skip to content

Commit 1eba2a0

Browse files
authored
Merge pull request #21 from spyoungtech/fix-unescaping
fix unwrap panics when unescaping invalid escape sequences in deserialization
2 parents 839a91b + a68d68c commit 1eba2a0

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

src/de.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,27 @@ impl<'de, 'a> Deserializer<'de> for JSONValueDeserializer<'a> {
4343
JSONValue::Null => visitor.visit_unit(),
4444
JSONValue::Bool(b) => visitor.visit_bool(*b),
4545
JSONValue::DoubleQuotedString(s) | JSONValue::SingleQuotedString(s) => {
46-
visitor.visit_str(unescape(s).unwrap().as_str())
46+
match unescape(s) {
47+
Ok(unescaped) => {
48+
visitor.visit_str(unescaped.as_str())
49+
}
50+
Err(e) => {
51+
Err(de::Error::custom(e.to_string()))
52+
}
53+
}
54+
4755
}
48-
JSONValue::Identifier(s) => visitor.visit_str(unescape(s).unwrap().as_str()),
56+
JSONValue::Identifier(s) => {
57+
match unescape(s) {
58+
Ok(unescaped) => {
59+
visitor.visit_str(unescaped.as_str())
60+
}
61+
Err(e) => {
62+
Err(de::Error::custom(e.to_string()))
63+
}
64+
}
65+
66+
},
4967
JSONValue::JSONObject { key_value_pairs } => {
5068
// Treat as a map
5169
let mut map_deserializer = JSONMapAccess {
@@ -556,7 +574,17 @@ impl<'de, 'a> Deserializer<'de> for JSONValueDeserializer<'a> {
556574
match self.input {
557575
JSONValue::Identifier(s)
558576
| JSONValue::DoubleQuotedString(s)
559-
| JSONValue::SingleQuotedString(s) => visitor.visit_str(unescape(s).unwrap().as_str()),
577+
| JSONValue::SingleQuotedString(s) => {
578+
match unescape(s) {
579+
Ok(unescaped) => {
580+
visitor.visit_str(unescaped.as_str())
581+
}
582+
Err(e) => {
583+
Err(de::Error::custom(e.to_string()))
584+
}
585+
}
586+
587+
},
560588
_ => self.deserialize_any(visitor),
561589
}
562590
}

0 commit comments

Comments
 (0)