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 UB in from_raw_parts_mut example #1492

Merged
merged 4 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions 2018-edition/src/ch19-01-unsafe-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ location and creates a slice 10,000 items long.
```rust
use std::slice;

let address = 0x012345usize;
let address = 0x01234usize;
let r = address as *mut i32;

let slice = unsafe {
let slice : &[i32] = unsafe {
slice::from_raw_parts_mut(r, 10000)
};
```
Expand All @@ -348,7 +348,11 @@ location</span>

We don’t own the memory at this arbitrary location, and there is no guarantee
that the slice this code creates contains valid `i32` values. Attempting to use
`slice` as though it’s a valid slice results in undefined behavior.
`slice` as though it’s a valid slice results in undefined behavior. If we would
not have taken care to align `address` to 4 (the alignment of `i32`), then even
just calling `slice::from_raw_parts_mut` would already be undefined behavior --
slices must always be aligned, even if they are not used (and even if they are
empty).

#### Using `extern` Functions to Call External Code

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ contributions should be to this edition.

### Second Edition

The second edition is frozen, and is not accepting any changes at this time.
No Starch Press has brought the second edition to print. Pull requests fixing
factual errors will be accepted and documented as errata; pull requests changing
wording or other small corrections should be made against the 2018 edition instead.

### First Edition

Expand Down
2 changes: 1 addition & 1 deletion second-edition/src/ch19-01-unsafe-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ location and creates a slice 10,000 items long.
```rust
use std::slice;

let address = 0x012345usize;
let address = 0x01234usize;
let r = address as *mut i32;

let slice = unsafe {
Expand Down