Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Use offset instead of add #5

Merged

Conversation

KamilaBorowska
Copy link
Contributor

This ensures compatibility with older Rust releases.

Fixes marshallpierce/rust-base64#112.

This ensures compatibility with older Rust releases.

Fixes marshallpierce/rust-base64#112.
@@ -44,7 +44,7 @@ pub fn copy_over<T: Copy>(slice: &mut [T], src_idx: usize, dest_idx: usize, len:
let ptr = slice.as_mut_ptr();

unsafe {
ptr::copy(ptr.add(src_idx), ptr.add(dest_idx), len);
ptr::copy(ptr.offset(src_idx as isize), ptr.offset(dest_idx as isize), len);
Copy link
Owner

@abonander abonander Aug 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check that this cast doesn't overflow? We already bounds-check these indices, and while the docs say that overflowing isize is a non-issue for allocations issued by the stdlib, and impossible on current x86_64 systems, they also point out that it could happen with manually allocated memory on systems with PAE.

I'm also aware that .add() also just naively casts to isize but I'm wondering if we want to be overly paranoid here and check all preconditions.

Copy link
Contributor Author

@KamilaBorowska KamilaBorowska Aug 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't an issue. Documentation for <[_]>::from_raw_parts outright says "The total size of the slice must be no larger than isize::MAX bytes in memory." Due to this, it's not possible to create a valid non-ZST slice that takes more than isize::MAX bytes in memory. Violating this will lead to UB, so it's not an issue for this crate.

Another way of getting a slice bigger than isize::MAX is by using CStr::from_ptr. This method doesn't have this requirement, but I believe that to be a bug in standard library (I will be proposing a request to change the documentation here soon).

For ZST types, it is possible to create a slice where .len() > isize::MAX. That said, while the cast to isize can make the value negative, because .offset is essentially a no-op for ZST, providing a wrong value to .offset is non-issue, after all, -50 * 0 == 0, so the pointer gets moved by 0 bytes, which is still in-bounds for ZST object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reported the documentation issue for CStr::from_ptr in rust-lang/rust#63590.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough.

@abonander abonander merged commit 65d2cb1 into abonander:master Aug 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

This crate broke Hyper v0.10's compatibility with Rust v1.24 (Debian latest)
3 participants