Skip to content

Commit b688314

Browse files
Fix big endian read/write
Co-authored-by: matthewjasper <[email protected]>
1 parent 91c9351 commit b688314

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
@@ -564,7 +564,7 @@ pub fn write_target_uint(
564564
// So we do not write all bytes of the u128, just the "payload".
565565
match endianness {
566566
Endian::Little => target.write(&data.to_le_bytes())?,
567-
Endian::Big => target.write(&data.to_be_bytes())?,
567+
Endian::Big => target.write(&data.to_be_bytes()[16 - target.len()..])?,
568568
};
569569
debug_assert!(target.len() == 0); // We should have filled the target buffer.
570570
Ok(())
@@ -575,12 +575,18 @@ pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, i
575575
// This u128 holds an "any-size uint" (since smaller uints can fits in it)
576576
let mut buf = [0u8; std::mem::size_of::<u128>()];
577577
// So we do not read exactly 16 bytes into the u128, just the "payload".
578-
source.read(&mut buf)?;
578+
let uint = match endianness {
579+
Endian::Little => {
580+
source.read(&mut buf)?;
581+
Ok(u128::from_le_bytes(buf))
582+
}
583+
Endian::Big => {
584+
source.read(&mut buf[16 - source.len()..])?;
585+
Ok(u128::from_be_bytes(buf))
586+
}
587+
};
579588
debug_assert!(source.len() == 0); // We should have consumed the source buffer.
580-
match endianness {
581-
Endian::Little => Ok(u128::from_le_bytes(buf)),
582-
Endian::Big => Ok(u128::from_be_bytes(buf)),
583-
}
589+
uint
584590
}
585591

586592
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)