Skip to content

Commit 2df552b

Browse files
Fix big endian read/write
Co-authored-by: matthewjasper <mjjasper1@gmail.com>
1 parent dc00eff commit 2df552b

File tree

1 file changed

+12
-6
lines changed
  • compiler/rustc_middle/src/mir/interpret

1 file changed

+12
-6
lines changed

compiler/rustc_middle/src/mir/interpret/mod.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ pub fn write_target_uint(
565565
// So we do not write all bytes of the u128, just the "payload".
566566
match endianness {
567567
Endian::Little => target.write(&data.to_le_bytes())?,
568-
Endian::Big => target.write(&data.to_be_bytes())?,
568+
Endian::Big => target.write(&data.to_be_bytes()[16 - target.len()..])?,
569569
};
570570
debug_assert!(target.len() == 0); // We should have filled the target buffer.
571571
Ok(())
@@ -576,12 +576,18 @@ pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, i
576576
// This u128 holds an "any-size uint" (since smaller uints can fits in it)
577577
let mut buf = [0u8; std::mem::size_of::<u128>()];
578578
// So we do not read exactly 16 bytes into the u128, just the "payload".
579-
source.read(&mut buf)?;
579+
let uint = match endianness {
580+
Endian::Little => {
581+
source.read(&mut buf)?;
582+
Ok(u128::from_le_bytes(buf))
583+
}
584+
Endian::Big => {
585+
source.read(&mut buf[16 - source.len()..])?;
586+
Ok(u128::from_be_bytes(buf))
587+
}
588+
};
580589
debug_assert!(source.len() == 0); // We should have consumed the source buffer.
581-
match endianness {
582-
Endian::Little => Ok(u128::from_le_bytes(buf)),
583-
Endian::Big => Ok(u128::from_be_bytes(buf)),
584-
}
590+
uint
585591
}
586592

587593
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)