Skip to content

Commit

Permalink
cipher: mark set_pos_unchecked private method as unsafe (#1346)
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov authored Sep 22, 2023
1 parent 94f57bb commit e1bdc05
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions cipher/src/stream_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ where
T::BlockSize::USIZE
}

/// Set buffer position without checking that it's smaller
/// than buffer size.
///
/// # Safety
/// `pos` MUST be smaller than `T::BlockSize::USIZE`.
#[inline]
fn set_pos_unchecked(&mut self, pos: usize) {
unsafe fn set_pos_unchecked(&mut self, pos: usize) {
debug_assert!(pos < T::BlockSize::USIZE);
self.pos = pos as u8;
}
Expand Down Expand Up @@ -124,7 +129,13 @@ where
let n = data.len();
if n < rem.len() {
data.xor_in2out(&rem[..n]);
self.set_pos_unchecked(pos + n);
// SAFETY: we have checked that `n` is less than length of `rem`,
// which is equal to buffer length minus `pos`, thus `pos + n` is
// less than buffer length and satisfies the `set_pos_unchecked`
// safety condition
unsafe {
self.set_pos_unchecked(pos + n);
}
return Ok(());
}
let (mut left, right) = data.split_at(rem.len());
Expand All @@ -140,7 +151,12 @@ where
self.core.write_keystream_block(&mut self.buffer);
leftover.xor_in2out(&self.buffer[..n]);
}
self.set_pos_unchecked(n);
// SAFETY: `into_chunks` always returns tail with size
// less than buffer length, thus `n` satisfies the `set_pos_unchecked`
// safety condition
unsafe {
self.set_pos_unchecked(n);
}

Ok(())
}
Expand Down

0 comments on commit e1bdc05

Please sign in to comment.