Skip to content

invalid_from_utf8_unchecked does not fail for include_bytes! #115208

Description

@oriongonza

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!

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Aug 25, 2023
  2. ShE3py commented on Aug 25, 2023

    @ShE3py
    Contributor

    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.

  3. added and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Sep 11, 2023
  4. changed the title [-]clippy::invalid_utf8_in_unchecked does not fail for include_bytes![/-] [+]`invalid_from_utf8_unchecked` does not fail for include_bytes![/+] on Sep 12, 2023
  5. oriongonza commented on Sep 20, 2023

    @oriongonza
    ContributorAuthor

    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?

  6. added a commit that references this issue on Sep 21, 2023
  7. added a commit that references this issue on Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions