Skip to content

Do not use LEB128 for encoding u16 and i16 #92314

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

Merged
merged 1 commit into from
Jan 3, 2022
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
23 changes: 15 additions & 8 deletions compiler/rustc_serialize/src/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ impl serialize::Encoder for Encoder {

#[inline]
fn emit_u16(&mut self, v: u16) -> EncodeResult {
write_leb128!(self, v, u16, write_u16_leb128)
self.data.extend_from_slice(&v.to_le_bytes());
Ok(())
}

#[inline]
Expand Down Expand Up @@ -123,7 +124,8 @@ impl serialize::Encoder for Encoder {

#[inline]
fn emit_i16(&mut self, v: i16) -> EncodeResult {
write_leb128!(self, v, i16, write_i16_leb128)
self.data.extend_from_slice(&v.to_le_bytes());
Ok(())
}

#[inline]
Expand Down Expand Up @@ -446,7 +448,7 @@ impl serialize::Encoder for FileEncoder {

#[inline]
fn emit_u16(&mut self, v: u16) -> FileEncodeResult {
file_encoder_write_leb128!(self, v, u16, write_u16_leb128)
self.write_all(&v.to_le_bytes())
}

#[inline]
Expand Down Expand Up @@ -476,13 +478,12 @@ impl serialize::Encoder for FileEncoder {

#[inline]
fn emit_i16(&mut self, v: i16) -> FileEncodeResult {
file_encoder_write_leb128!(self, v, i16, write_i16_leb128)
self.write_all(&v.to_le_bytes())
}

#[inline]
fn emit_i8(&mut self, v: i8) -> FileEncodeResult {
let as_u8: u8 = unsafe { std::mem::transmute(v) };
self.emit_u8(as_u8)
self.emit_u8(v as u8)
}

#[inline]
Expand Down Expand Up @@ -591,7 +592,10 @@ impl<'a> serialize::Decoder for Decoder<'a> {

#[inline]
fn read_u16(&mut self) -> Result<u16, Self::Error> {
read_leb128!(self, read_u16_leb128)
let bytes = [self.data[self.position], self.data[self.position + 1]];
let value = u16::from_le_bytes(bytes);
self.position += 2;
Ok(value)
}

#[inline]
Expand Down Expand Up @@ -623,7 +627,10 @@ impl<'a> serialize::Decoder for Decoder<'a> {

#[inline]
fn read_i16(&mut self) -> Result<i16, Self::Error> {
read_leb128!(self, read_i16_leb128)
let bytes = [self.data[self.position], self.data[self.position + 1]];
let value = i16::from_le_bytes(bytes);
self.position += 2;
Ok(value)
}

#[inline]
Expand Down