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

Unsound usages of unsafe implementation from u8 to str #376

Closed
llooFlashooll opened this issue May 26, 2024 · 2 comments
Closed

Unsound usages of unsafe implementation from u8 to str #376

llooFlashooll opened this issue May 26, 2024 · 2 comments

Comments

@llooFlashooll
Copy link

Hi, I am scanning the anyhow in latest version with my own static analyzer tool.

Unsafe conversion found at: src/ensure.rs:50:38: 53:14.

fn as_str(&self) -> &str {
    unsafe {
        str::from_utf8_unchecked(slice::from_raw_parts(
            self.bytes.as_ptr().cast::<u8>(),
            self.written,
        ))
    }
}

This unsound implementation would create a misalignment issues.

This would potentially cause undefined behaviors in Rust. I am reporting this issue for your attention.

@dtolnay
Copy link
Owner

dtolnay commented May 26, 2024

I believe this code is correct. Str's alignment is 1 so I don't see how there would be the misalignment issue you say.

@dtolnay dtolnay closed this as completed May 26, 2024
@llooFlashooll
Copy link
Author

Ok, thanks! Since I think Rust it's a strongly-typed language, I think direct different type conversion, originally it should be an issue.
I am just reporting for discuss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants