Document unsafety in src/libcore/hash/sip.rs

This commit is contained in:
LeSeulArtichaut 2020-04-12 16:47:57 +02:00
parent d1ce7ff84e
commit b84f981361

View File

@ -1,7 +1,5 @@
//! An implementation of SipHash.
// ignore-tidy-undocumented-unsafe
#![allow(deprecated)] // the types in this module are deprecated
use crate::cmp;
@ -265,6 +263,7 @@ fn write(&mut self, msg: &[u8]) {
if self.ntail != 0 {
needed = 8 - self.ntail;
// SAFETY: `cmp::min(length, needed)` is guaranteed to not be over `length`
self.tail |= unsafe { u8to64_le(msg, 0, cmp::min(length, needed)) } << (8 * self.ntail);
if length < needed {
self.ntail += length;
@ -279,10 +278,13 @@ fn write(&mut self, msg: &[u8]) {
// Buffered tail is now flushed, process new input.
let len = length - needed;
let left = len & 0x7;
let left = len & 0x7; // len % 8
let mut i = needed;
while i < len - left {
// SAFETY: because `len - left` is the biggest multiple of 8 under
// `len`, and because `i` starts at `needed` where `len` is `length - needed`,
// `i + 8` is guaranteed to be less than or equal to `length`.
let mi = unsafe { load_int_le!(msg, i, u64) };
self.state.v3 ^= mi;
@ -292,6 +294,9 @@ fn write(&mut self, msg: &[u8]) {
i += 8;
}
// SAFETY: `i` is now `needed + len.div_euclid(8) * 8`,
// so `i + left` = `needed + len` = `length`, which is by
// definition equal to `msg.len()`.
self.tail = unsafe { u8to64_le(msg, i, left) };
self.ntail = left;
}