about summary refs log tree commit diff
path: root/compiler/rustc_data_structures/src
diff options
context:
space:
mode:
authorJakub Beránek <berykubik@gmail.com>2021-12-18 20:41:58 +0100
committerJakub Beránek <berykubik@gmail.com>2022-01-04 19:12:10 +0100
commit65a3279f4a221e97c97ca1c49d89626c8700d017 (patch)
tree65001aee1a9dfd9850261c8e060b4ef950e1efaa /compiler/rustc_data_structures/src
parentb5da80871d5e22401e03ce5ed73200ece8bdc7a6 (diff)
downloadrust-65a3279f4a221e97c97ca1c49d89626c8700d017.tar.gz
rust-65a3279f4a221e97c97ca1c49d89626c8700d017.zip
Do not hash zero bytes of i64 and u32 in Sip128 hasher
Diffstat (limited to 'compiler/rustc_data_structures/src')
-rw-r--r--compiler/rustc_data_structures/src/sip128.rs18
1 files changed, 16 insertions, 2 deletions
diff --git a/compiler/rustc_data_structures/src/sip128.rs b/compiler/rustc_data_structures/src/sip128.rs
index 53062b9c20d..872b0eb7854 100644
--- a/compiler/rustc_data_structures/src/sip128.rs
+++ b/compiler/rustc_data_structures/src/sip128.rs
@@ -409,6 +409,20 @@ impl SipHasher128 {
     }
 }
 
+macro_rules! dispatch_value {
+    ($target: expr, $value:expr) => {
+        let value = $value;
+        #[allow(unreachable_patterns)]
+        #[allow(overflowing_literals)]
+        match value {
+            0..=0xFF => $target.short_write(value as u8),
+            0x100..=0xFFFF => $target.short_write(value as u16),
+            0x10000..=0xFFFFFFFF => $target.short_write(value as u32),
+            _ => $target.short_write(value as u64),
+        }
+    };
+}
+
 impl Hasher for SipHasher128 {
     #[inline]
     fn write_u8(&mut self, i: u8) {
@@ -422,7 +436,7 @@ impl Hasher for SipHasher128 {
 
     #[inline]
     fn write_u32(&mut self, i: u32) {
-        self.short_write(i);
+        dispatch_value!(self, i);
     }
 
     #[inline]
@@ -452,7 +466,7 @@ impl Hasher for SipHasher128 {
 
     #[inline]
     fn write_i64(&mut self, i: i64) {
-        self.short_write(i as u64);
+        dispatch_value!(self, i as u64);
     }
 
     #[inline]