Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions compiler/rustc_serialize/src/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ macro_rules! write_leb128 {
}};
}

/// A byte that [cannot occur in UTF8 sequences][utf8]. Used to mark the end of a string.
/// This way we can skip validation and still be relatively sure that deserialization
/// did not desynchronize.
///
/// [utf8]: https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=1058865525#Codepage_layout
const STR_SENTINEL: u8 = 0xC1;

impl serialize::Encoder for Encoder {
type Error = !;

Expand Down Expand Up @@ -150,7 +157,8 @@ impl serialize::Encoder for Encoder {
#[inline]
fn emit_str(&mut self, v: &str) -> EncodeResult {
self.emit_usize(v.len())?;
self.emit_raw_bytes(v.as_bytes())
self.emit_raw_bytes(v.as_bytes())?;
self.emit_u8(STR_SENTINEL)
}

#[inline]
Expand Down Expand Up @@ -502,7 +510,8 @@ impl serialize::Encoder for FileEncoder {
#[inline]
fn emit_str(&mut self, v: &str) -> FileEncodeResult {
self.emit_usize(v.len())?;
self.emit_raw_bytes(v.as_bytes())
self.emit_raw_bytes(v.as_bytes())?;
self.emit_u8(STR_SENTINEL)
}

#[inline]
Expand Down Expand Up @@ -656,8 +665,12 @@ impl<'a> serialize::Decoder for Decoder<'a> {
#[inline]
fn read_str(&mut self) -> Result<Cow<'_, str>, Self::Error> {
let len = self.read_usize()?;
let s = std::str::from_utf8(&self.data[self.position..self.position + len]).unwrap();
self.position += len;
let sentinel = self.data[self.position + len];
assert!(sentinel == STR_SENTINEL);
let s = unsafe {
std::str::from_utf8_unchecked(&self.data[self.position..self.position + len])
};
self.position += len + 1;
Ok(Cow::Borrowed(s))
}

Expand Down