Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unsoundness in <BlockRng64 as RngCore>::next_u32, with less unsafe code #1160

Merged
merged 22 commits into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ A [separate changelog is kept for rand_core](rand_core/CHANGELOG.md).

You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.html) useful.

## [0.8.5] - unreleased
## [0.8.5] - 2021-08-20
### Fixes
- Fix build on non-32/64-bit architectures (#1144)
- Fix build on non-32/64-bit architectures (#1144)

### Platform support
- Remove special cases for emscripten (#1142)

### Documentation
- Added docs about rand's use of const generics (#1150)
- Better random chars example (#1157)

## [0.8.4] - 2021-06-15
### Additions
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rand"
version = "0.8.4"
version = "0.8.5"
authors = ["The Rand Project Developers", "The Rust Project Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand Down
4 changes: 4 additions & 0 deletions rand_core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.6.4] - 2021-08-20
### Fixed
- Fix unsoundness in `<BlockRng64 as RngCore>::next_u32` (#1160)

## [0.6.3] - 2021-06-15
### Changed
- Improved bound for `serde` impls on `BlockRng` (#1130)
Expand Down
2 changes: 1 addition & 1 deletion rand_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rand_core"
version = "0.6.3"
version = "0.6.4"
authors = ["The Rand Project Developers", "The Rust Project Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand Down
19 changes: 7 additions & 12 deletions rand_core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,27 +352,22 @@ where
{
#[inline]
fn next_u32(&mut self) -> u32 {
let mut index = self.index * 2 - self.half_used as usize;
if index >= self.results.as_ref().len() * 2 {
if self.index >= self.results.as_ref().len() {
vks marked this conversation as resolved.
Show resolved Hide resolved
self.core.generate(&mut self.results);
self.index = 0;
// `self.half_used` is by definition `false`
self.half_used = false;
index = 0;
}

self.half_used = !self.half_used;
self.index += self.half_used as usize;

// Index as if this is a u32 slice.
unsafe {
let results = &*(self.results.as_ref() as *const [u64] as *const [u32]);
if cfg!(target_endian = "little") {
*results.get_unchecked(index)
} else {
*results.get_unchecked(index ^ 1)
}
}
let half_used = if cfg!(target_endian = "little") {
self.half_used
} else {
!self.half_used
};
vks marked this conversation as resolved.
Show resolved Hide resolved
(self.results.as_ref()[self.index] >> (32 * (half_used as usize))) as u32
}

#[inline]
Expand Down