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

Add valid examples to std::ptr::{read,write}_unaligned #62357

Closed
Centril opened this issue Jul 4, 2019 · 4 comments
Closed

Add valid examples to std::ptr::{read,write}_unaligned #62357

Centril opened this issue Jul 4, 2019 · 4 comments
Assignees
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@Centril
Copy link
Contributor

Centril commented Jul 4, 2019

With #62323 the only example (that had UB and was thus invalid) in std::ptr::read_unaligned and std::ptr::write_unaligned is removed.

We should add a valid example of using the aforementioned functions.

cc @rust-lang/wg-unsafe-code-guidelines

This issue has been assigned to @Freyskeyd via this comment.

@Centril Centril added A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Jul 4, 2019
@RalfJung
Copy link
Member

RalfJung commented Jul 4, 2019

Slices are one context in which one could conceivably create an unaligned raw pointer:

fn read_usize(x: &[u8]) -> usize {
  assert!(x.len() >= mem::size_of::<usize>());
  let ptr = x.as_ptr() as *const usize;
  unsafe { ptr.read_unaligned() }
}

Thanks to @Shnatsel on Zulip.

The fix for this bug thus is to add an example like this, and a similar one for writes, to the docs for read_unaligned and write_unaligned. The docs should also explain why using just read or write is UB.

@RalfJung RalfJung added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Jul 4, 2019
@Freyskeyd
Copy link
Contributor

Hello!

I would like to work on this one :)

@Freyskeyd
Copy link
Contributor

@rustbot claim

@rustbot rustbot self-assigned this Jul 8, 2019
Centril added a commit to Centril/rust that referenced this issue Jul 10, 2019
…unaligned, r=rkruppe

rust-lang#62357: doc(ptr): add example for {read,write}_unaligned

related to rust-lang#62357

> With rust-lang#62323 the only example (that had UB and was thus invalid) in std::ptr::read_unaligned and std::ptr::write_unaligned is removed.

> We should add a valid example of using the aforementioned functions.

Signed-off-by: Freyskeyd <simon.paitrault@gmail.com>
bors added a commit that referenced this issue Jul 10, 2019
Rollup of 5 pull requests

Successful merges:

 - #62275 (rustc_mir: treat DropAndReplace as Drop + Assign in qualify_consts.)
 - #62465 (Sometimes generate storage statements for temporaries with type `!`)
 - #62481 (Use `fold` in `Iterator::last` default implementation)
 - #62493 (#62357: doc(ptr): add example for {read,write}_unaligned)
 - #62532 (Some more cleanups to syntax::print)

Failed merges:

r? @ghost
bors added a commit that referenced this issue Jul 10, 2019
Rollup of 5 pull requests

Successful merges:

 - #62275 (rustc_mir: treat DropAndReplace as Drop + Assign in qualify_consts.)
 - #62465 (Sometimes generate storage statements for temporaries with type `!`)
 - #62481 (Use `fold` in `Iterator::last` default implementation)
 - #62493 (#62357: doc(ptr): add example for {read,write}_unaligned)
 - #62532 (Some more cleanups to syntax::print)

Failed merges:

r? @ghost
@RalfJung
Copy link
Member

Fixed by #62493.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

4 participants