Skip to content

Commit 5c11a5e

Browse files
committed
Remove unsound Raw feature
1 parent 36940d0 commit 5c11a5e

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

rmp-serde/src/lib.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,13 @@
5555
//! ```
5656
//!
5757
//! [serde]: https://serde.rs/
58-
58+
#![forbid(unsafe_code)]
5959
#![warn(missing_debug_implementations, missing_docs)]
6060

6161
#[macro_use]
6262
extern crate serde;
6363

6464
use std::fmt::{self, Display, Formatter};
65-
use std::mem;
6665
use std::str::{self, Utf8Error};
6766

6867
use serde::de;
@@ -99,6 +98,7 @@ pub const MSGPACK_EXT_STRUCT_NAME: &str = "_ExtStruct";
9998
///
10099
/// Regardless of validity the UTF-8 content this type will always be serialized as a string.
101100
#[derive(Clone, Debug, PartialEq)]
101+
#[doc(hidden)]
102102
pub struct Raw {
103103
s: Result<String, (Vec<u8>, Utf8Error)>,
104104
}
@@ -111,7 +111,7 @@ impl Raw {
111111
}
112112

113113
/// DO NOT USE. See <https://github.com/3Hren/msgpack-rust/issues/305>
114-
#[deprecated(note = "This implementation is unsound and dangerous. See https://github.com/3Hren/msgpack-rust/issues/305")]
114+
#[deprecated(note = "This feature has been removed")]
115115
pub fn from_utf8(v: Vec<u8>) -> Self {
116116
match String::from_utf8(v) {
117117
Ok(v) => Raw::new(v),
@@ -185,14 +185,10 @@ impl Serialize for Raw {
185185
where
186186
S: serde::Serializer
187187
{
188-
let s = match self.s {
189-
Ok(ref s) => s.as_str(),
190-
// FIXME: this is invalid. It should use a newtype hack instead.
191-
// https://github.com/3Hren/msgpack-rust/issues/305
192-
Err((ref b, ..)) => unsafe { mem::transmute(&b[..]) },
193-
};
194-
195-
se.serialize_str(s)
188+
match self.s {
189+
Ok(ref s) => se.serialize_str(s),
190+
Err((ref b, ..)) => se.serialize_bytes(b),
191+
}
196192
}
197193
}
198194

@@ -260,6 +256,7 @@ impl<'de> Deserialize<'de> for Raw {
260256
///
261257
/// Regardless of validity the UTF-8 content this type will always be serialized as a string.
262258
#[derive(Clone, Copy, Debug, PartialEq)]
259+
#[doc(hidden)]
263260
pub struct RawRef<'a> {
264261
s: Result<&'a str, (&'a [u8], Utf8Error)>,
265262
}
@@ -271,8 +268,7 @@ impl<'a> RawRef<'a> {
271268
Self { s: Ok(v) }
272269
}
273270

274-
/// DO NOT USE. See <https://github.com/3Hren/msgpack-rust/issues/305>
275-
#[deprecated(note = "This implementation is unsound and dangerous. See https://github.com/3Hren/msgpack-rust/issues/305")]
271+
#[deprecated(note = "This feature has been removed")]
276272
pub fn from_utf8(v: &'a [u8]) -> Self {
277273
match str::from_utf8(v) {
278274
Ok(v) => RawRef::new(v),
@@ -330,14 +326,10 @@ impl<'a> Serialize for RawRef<'a> {
330326
where
331327
S: serde::Serializer,
332328
{
333-
let s = match self.s {
334-
Ok(ref s) => s,
335-
// FIXME: this is invalid. It should use a newtype hack instead.
336-
// https://github.com/3Hren/msgpack-rust/issues/305
337-
Err((ref b, ..)) => unsafe { mem::transmute(b) },
338-
};
339-
340-
se.serialize_str(s)
329+
match self.s {
330+
Ok(ref s) => se.serialize_str(s),
331+
Err((ref b, ..)) => se.serialize_bytes(b),
332+
}
341333
}
342334
}
343335

rmp-serde/tests/encode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ fn pass_raw_invalid_utf8() {
337337
let mut buf = Vec::new();
338338
raw.serialize(&mut Serializer::new(&mut buf)).unwrap();
339339

340-
assert_eq!(vec![0xa4, 0x92, 0xcc, 0xc8, 0x90], buf);
340+
assert_eq!(vec![196, 4, 146, 204, 200, 144], buf);
341341
}
342342

343343
#[test]
@@ -361,7 +361,7 @@ fn pass_raw_ref_invalid_utf8() {
361361
let mut buf = Vec::new();
362362
raw.serialize(&mut Serializer::new(&mut buf)).unwrap();
363363

364-
assert_eq!(vec![0xa4, 0x92, 0xcc, 0xc8, 0x90], buf);
364+
assert_eq!(vec![196, 4, 146, 204, 200, 144], buf);
365365
}
366366

367367
#[test]

rmpv/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//!
55
//! ```
66
//! ```
7+
#![forbid(unsafe_code)]
78

89
#[cfg(feature = "with-serde")]
910
#[macro_use]

0 commit comments

Comments
 (0)