diff --git a/src/error.rs b/src/error.rs index 17fee05..ac31efd 100644 --- a/src/error.rs +++ b/src/error.rs @@ -147,18 +147,11 @@ pub enum DecodeError { duration: core::time::Duration, }, - /// The decoder tried to decode a `CStr`, but the incoming data contained a 0 byte - #[cfg(feature = "std")] - CStrNulError { - /// The inner exception - inner: std::ffi::FromBytesWithNulError, - }, - /// The decoder tried to decode a `CString`, but the incoming data contained a 0 byte #[cfg(feature = "std")] CStringNulError { /// The inner exception - inner: std::ffi::FromVecWithNulError, + inner: std::ffi::NulError, }, /// An uncommon error occurred, see the inner text for more information diff --git a/src/features/impl_std.rs b/src/features/impl_std.rs index 811148e..2bac0ff 100644 --- a/src/features/impl_std.rs +++ b/src/features/impl_std.rs @@ -125,27 +125,20 @@ impl<'storage, W: std::io::Write> Writer for IoWriter<'storage, W> { impl<'a> Encode for &'a CStr { fn encode(&self, encoder: &mut E) -> Result<(), EncodeError> { - self.to_bytes_with_nul().encode(encoder) - } -} - -impl<'de> BorrowDecode<'de> for &'de CStr { - fn borrow_decode>(decoder: &mut D) -> Result { - let bytes = <&[u8]>::borrow_decode(decoder)?; - CStr::from_bytes_with_nul(bytes).map_err(|e| DecodeError::CStrNulError { inner: e }) + self.to_bytes().encode(encoder) } } impl Encode for CString { fn encode(&self, encoder: &mut E) -> Result<(), EncodeError> { - self.as_bytes_with_nul().encode(encoder) + self.as_bytes().encode(encoder) } } impl Decode for CString { fn decode(decoder: &mut D) -> Result { let vec = std::vec::Vec::decode(decoder)?; - CString::from_vec_with_nul(vec).map_err(|inner| DecodeError::CStringNulError { inner }) + CString::new(vec).map_err(|inner| DecodeError::CStringNulError { inner }) } } diff --git a/tests/issues.rs b/tests/issues.rs index 40c5342..b1d2a94 100644 --- a/tests/issues.rs +++ b/tests/issues.rs @@ -14,3 +14,6 @@ mod issue_459; #[path = "issues/issue_474.rs"] mod issue_474; + +#[path = "issues/issue_498.rs"] +mod issue_498; diff --git a/tests/issues/issue_498.rs b/tests/issues/issue_498.rs new file mode 100644 index 0000000..a60fec8 --- /dev/null +++ b/tests/issues/issue_498.rs @@ -0,0 +1,17 @@ +#![cfg(feature = "std")] + +extern crate std; + +use std::ffi::CString; + +#[test] +fn test_issue_498() { + let bytes = [1, 0, 0, 0, 0, 0, 0, 0, 0]; + let out: Result<(CString, _), _> = + bincode::decode_from_slice(&bytes, bincode::config::legacy().with_limit::<1024>()); + + match out.unwrap_err() { + bincode::error::DecodeError::CStringNulError { inner: _ } => {} + err => panic!("Expected CStringNullErr, found {:?}", err), + } +} diff --git a/tests/std.rs b/tests/std.rs index 316d41d..528fa1f 100644 --- a/tests/std.rs +++ b/tests/std.rs @@ -108,10 +108,10 @@ fn test_std_commons() { // &CStr let cstr = CStr::from_bytes_with_nul(b"Hello world\0").unwrap(); let len = bincode::encode_into_slice(cstr, &mut buffer, config).unwrap(); - let (decoded, len): (&CStr, usize) = + let (decoded, len): (CString, usize) = bincode::decode_from_slice(&mut buffer[..len], config).unwrap(); - assert_eq!(cstr, decoded); - assert_eq!(len, 13); + assert_eq!(cstr, decoded.as_c_str()); + assert_eq!(len, 12); // Path let path = Path::new("C:/Program Files/Foo");