Skip to content

Commit 539b009

Browse files
committed
use start_position to handle little/big endians
Signed-off-by: onur-ozkan <[email protected]>
1 parent d5b98ff commit 539b009

File tree

2 files changed

+9
-6
lines changed
  • compiler

2 files changed

+9
-6
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -668,14 +668,15 @@ pub fn write_target_uint(
668668
pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, io::Error> {
669669
// This u128 holds an "any-size uint" (since smaller uints can fits in it)
670670
let mut buf = [0u8; std::mem::size_of::<u128>()];
671+
let start_position = 16 - source.len();
671672
// So we do not read exactly 16 bytes into the u128, just the "payload".
672673
let uint = match endianness {
673674
Endian::Little => {
674-
let _ = source.read(&mut buf)?;
675+
source.read_exact(&mut buf[..start_position])?;
675676
Ok(u128::from_le_bytes(buf))
676677
}
677678
Endian::Big => {
678-
source.read_exact(&mut buf[16 - source.len()..])?;
679+
source.read_exact(&mut buf[start_position..])?;
679680
Ok(u128::from_be_bytes(buf))
680681
}
681682
};

compiler/stable_mir/src/mir/alloc.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ impl IndexedVal for AllocId {
5555
/// Utility function used to read an allocation data into a unassigned integer.
5656
pub(crate) fn read_target_uint(mut bytes: &[u8]) -> Result<u128, Error> {
5757
let mut buf = [0u8; std::mem::size_of::<u128>()];
58+
let start_position = 16 - bytes.len();
5859
match MachineInfo::target_endianess() {
5960
Endian::Little => {
60-
let _ = bytes.read(&mut buf)?;
61+
bytes.read_exact(&mut buf[..start_position])?;
6162
Ok(u128::from_le_bytes(buf))
6263
}
6364
Endian::Big => {
64-
bytes.read_exact(&mut buf[16 - bytes.len()..])?;
65+
bytes.read_exact(&mut buf[start_position..])?;
6566
Ok(u128::from_be_bytes(buf))
6667
}
6768
}
@@ -70,13 +71,14 @@ pub(crate) fn read_target_uint(mut bytes: &[u8]) -> Result<u128, Error> {
7071
/// Utility function used to read an allocation data into an assigned integer.
7172
pub(crate) fn read_target_int(mut bytes: &[u8]) -> Result<i128, Error> {
7273
let mut buf = [0u8; std::mem::size_of::<i128>()];
74+
let start_position = 16 - bytes.len();
7375
match MachineInfo::target_endianess() {
7476
Endian::Little => {
75-
let _ = bytes.read(&mut buf)?;
77+
bytes.read_exact(&mut buf[..start_position])?;
7678
Ok(i128::from_le_bytes(buf))
7779
}
7880
Endian::Big => {
79-
bytes.read_exact(&mut buf[16 - bytes.len()..])?;
81+
bytes.read_exact(&mut buf[start_position..])?;
8082
Ok(i128::from_be_bytes(buf))
8183
}
8284
}

0 commit comments

Comments
 (0)