about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJubilee Young <workingjubilee@gmail.com>2020-08-22 03:54:15 -0700
committerJubilee Young <workingjubilee@gmail.com>2020-09-04 21:51:29 -0700
commit2df552b406d8794dc0617672b34e1542498fd0ce (patch)
tree05a8e9aa14713acaa9b2b58b167319df811e9568
parentdc00efff9f44ddda1ce318e28f69e716f58d39dd (diff)
downloadrust-2df552b406d8794dc0617672b34e1542498fd0ce.tar.gz
rust-2df552b406d8794dc0617672b34e1542498fd0ce.zip
Fix big endian read/write
Co-authored-by: matthewjasper <mjjasper1@gmail.com>
-rw-r--r--compiler/rustc_middle/src/mir/interpret/mod.rs18
1 files changed, 12 insertions, 6 deletions
diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs
index 99966121e4d..cbc362d934f 100644
--- a/compiler/rustc_middle/src/mir/interpret/mod.rs
+++ b/compiler/rustc_middle/src/mir/interpret/mod.rs
@@ -565,7 +565,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(())
@@ -576,12 +576,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
 }
 
 ////////////////////////////////////////////////////////////////////////////////