Repository: serde-rs/json (language: rust)

Issue:
Panic on input that is not valid UTF8 when deserializing to a RawValue
Very similar to this issue: https://github.com/serde-rs/json/issues/99

However, I ran into this problem when deserializing to a RawValue.

```
let json_str2 = &[ b'"', b'\xCE', b'\xF8', b'"'];
let v2: serde_json::Result<Box<serde_json::value::RawValue>> = serde_json::from_slice(json_str2);
```

I get this kind of backtrace:

```
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Utf8Error { valid_up_to: 1, error_len: Some(1) }', C:\Users\X\.cargo\registry\src\github.com-1ecc6299db9ec823\serde_json-1.0.63\src\read.rs:590:39
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\/library\std\src\panicking.rs:493
   1: core::panicking::panic_fmt
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\/library\core\src\panicking.rs:92
   2: core::option::expect_none_failed
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b\/library\core\src\option.rs:1268
   3: core::result::Result<str, core::str::error::Utf8Error>::unwrap<str,core::str::error::Utf8Error>
             at C:\Users\X\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\result.rs:973
   4: serde_json::read::{{impl}}::end_raw_buffering<serde_json::raw::{{impl}}::deserialize::BoxedVisitor>
             at C:\Users\X\.cargo\registry\src\github.com-1ecc6299db9ec823\serde_json-1.0.63\src\read.rs:590
   5: serde_json::de::Deserializer<serde_json::read::SliceRead>::deserialize_raw_value<serde_json::read::SliceRead,serde_json::raw::{{impl}}::deserialize::BoxedVisitor>
             at C:\Users\X\.cargo\registry\src\github.com-1ecc6299db9ec823\serde_json-1.0.63\src\de.rs:1194
   6: serde_json::de::{{impl}}::deserialize_newtype_struct<serde_json::read::SliceRead,serde_json::raw::{{impl}}::deserialize::BoxedVisitor>
             at C:\Users\X\.cargo\registry\src\github.com-1ecc6299db9ec823\serde_json-1.0.63\src\de.rs:1695
   7: serde_json::raw::{{impl}}::deserialize<mut serde_json::de::Deserializer<serde_json::read::SliceRead>*>
             at C:\Users\X\.cargo\registry\src\github.com-1ecc6299db9ec823\serde_json-1.0.63\src\raw.rs:347
   8: serde_json::de::from_trait<serde_json::read::SliceRead,alloc::boxed::Box<serde_json::raw::RawValue, alloc::alloc::Global>>
             at C:\Users\X\.cargo\registry\src\github.com-1ecc6299db9ec823\serde_json-1.0.63\src\de.rs:2386
   9: serde_json::de::from_slice<alloc::boxed::Box<serde_json::raw::RawValue, alloc::alloc::Global>>
             at C:\Users\X\.cargo\registry\src\github.com-1ecc6299db9ec823\serde_json-1.0.63\src\de.rs:2544
  10: gltf_data_packer::main
             at .\src\main.rs:34
  11: core::ops::function::FnOnce::call_once<fn(),tuple<>>
             at C:\Users\X\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:227
```

I encountered this issue when parsing a glTF-file: https://github.com/gltf-rs/gltf/issues/298


Discussion / hints:

Fix patch:
diff --git a/src/read.rs b/src/read.rs
index 522c0e011..574857cd8 100644
--- a/src/read.rs
+++ b/src/read.rs
@@ -587,7 +587,10 @@ impl<'a> Read<'a> for SliceRead<'a> {
         V: Visitor<'a>,
     {
         let raw = &self.slice[self.raw_buffering_start_index..self.index];
-        let raw = str::from_utf8(raw).unwrap();
+        let raw = match str::from_utf8(raw) {
+            Ok(raw) => raw,
+            Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
+        };
         visitor.visit_map(BorrowedRawDeserializer {
             raw_value: Some(raw),
         })


Test patch:
diff --git a/tests/test.rs b/tests/test.rs
index ffe58c979..2fe7c560a 100644
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -2201,6 +2201,25 @@ fn test_boxed_raw_value() {
     assert_eq!(r#"["a",42,{"foo": "bar"},null]"#, array_to_string);
 }
 
+#[cfg(feature = "raw_value")]
+#[test]
+fn test_raw_invalid_utf8() {
+    use serde_json::value::RawValue;
+
+    let j = &[b'"', b'\xCE', b'\xF8', b'"'];
+    let value_err = serde_json::from_slice::<Value>(j).unwrap_err();
+    let raw_value_err = serde_json::from_slice::<Box<RawValue>>(j).unwrap_err();
+
+    assert_eq!(
+        value_err.to_string(),
+        "invalid unicode code point at line 1 column 4",
+    );
+    assert_eq!(
+        raw_value_err.to_string(),
+        "invalid unicode code point at line 1 column 4",
+    );
+}
+
 #[test]
 fn test_borrow_in_map_key() {
     #[derive(Deserialize, Debug)]