Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert Buf::as_str safety change from PR 2319 #2329

Merged
merged 3 commits into from Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion serde/src/de/format.rs
@@ -1,7 +1,7 @@
use lib::fmt::{self, Write};
use lib::str;

pub struct Buf<'a> {
pub(super) struct Buf<'a> {
bytes: &'a mut [u8],
offset: usize,
}
Expand Down
5 changes: 3 additions & 2 deletions serde/src/ser/impls.rs
Expand Up @@ -736,8 +736,9 @@ impl Serialize for net::Ipv4Addr {
// Skip over delimiters that we initialized buf with
written += format_u8(*oct, &mut buf[written + 1..]) + 1;
}
// We've only written ASCII bytes to the buffer, so it is valid UTF-8
serializer.serialize_str(unsafe { str::from_utf8_unchecked(&buf[..written]) })
// Safety: We've only written ASCII bytes to the buffer, so it is valid UTF-8
let buf = unsafe { str::from_utf8_unchecked(&buf[..written]) };
serializer.serialize_str(buf)
} else {
self.octets().serialize(serializer)
}
Expand Down