Skip to content

Commit

Permalink
Fix big endian read/write
Browse files Browse the repository at this point in the history
Co-authored-by: matthewjasper <mjjasper1@gmail.com>
  • Loading branch information
workingjubilee and matthewjasper committed Aug 31, 2020
1 parent 91c9351 commit b688314
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions compiler/rustc_middle/src/mir/interpret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ pub fn write_target_uint(
// So we do not write all bytes of the u128, just the "payload".
match endianness {
Endian::Little => target.write(&data.to_le_bytes())?,
Endian::Big => target.write(&data.to_be_bytes())?,
Endian::Big => target.write(&data.to_be_bytes()[16 - target.len()..])?,
};
debug_assert!(target.len() == 0); // We should have filled the target buffer.
Ok(())
Expand All @@ -575,12 +575,18 @@ pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, i
// This u128 holds an "any-size uint" (since smaller uints can fits in it)
let mut buf = [0u8; std::mem::size_of::<u128>()];
// So we do not read exactly 16 bytes into the u128, just the "payload".
source.read(&mut buf)?;
let uint = match endianness {
Endian::Little => {
source.read(&mut buf)?;
Ok(u128::from_le_bytes(buf))
}
Endian::Big => {
source.read(&mut buf[16 - source.len()..])?;
Ok(u128::from_be_bytes(buf))
}
};
debug_assert!(source.len() == 0); // We should have consumed the source buffer.
match endianness {
Endian::Little => Ok(u128::from_le_bytes(buf)),
Endian::Big => Ok(u128::from_be_bytes(buf)),
}
uint
}

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit b688314

Please sign in to comment.