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

invalid_from_utf8_unchecked does not fail for include_bytes! #115208

Closed
dev-ardi opened this issue Aug 25, 2023 · 2 comments · Fixed by #115257
Closed

invalid_from_utf8_unchecked does not fail for include_bytes! #115208

dev-ardi opened this issue Aug 25, 2023 · 2 comments · Fixed by #115257
Labels
C-bug Category: This is a bug.

Comments

@dev-ardi
Copy link
Contributor

dev-ardi commented Aug 25, 2023

This code should fail if invalid.txt contains invalid UTF-8.

let str = include_bytes!("../invalid.txt");
let str = unsafe { std::str::from_utf8_unchecked(str) };

This should fail as the byte sequence is known at compile time!

@dev-ardi dev-ardi added the C-bug Category: This is a bug. label Aug 25, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 25, 2023
@ShE3py
Copy link
Contributor

ShE3py commented Aug 25, 2023

Isn't this the purpose of include_str!() ? I think Clippy should deny include_bytes!() on text files, as str::as_bytes() is a no-op.

@saethlin saethlin added A-clippy Area: Clippy and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 11, 2023
@Alexendoo Alexendoo removed the A-clippy Area: Clippy label Sep 12, 2023
@Alexendoo Alexendoo changed the title clippy::invalid_utf8_in_unchecked does not fail for include_bytes! invalid_from_utf8_unchecked does not fail for include_bytes! Sep 12, 2023
@dev-ardi
Copy link
Contributor Author

dev-ardi commented Sep 20, 2023

Isn't this the purpose of include_str!()?

That's not the point. A literal sequence of bytes known at compile time should fail if invalid utf8 is found, no matter how it's loaded.

I think Clippy should deny include_bytes!() on text files, as str::as_bytes() is a no-op.

What's a text file? How can you tell it apart from any other file?

@bors bors closed this as completed in 9ce64ba Sep 21, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 21, 2023
Rollup merge of rust-lang#115257 - Urgau:invalid-utf8-walk-up-hir, r=Nilstrieb

Improve invalid UTF-8 lint by finding the expression initializer

This PR introduce a small mechanism to walk up the HIR through bindings, if/else, consts, ... when trying lint on invalid UTF-8.

Fixes rust-lang#115208
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants