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

Replace unsafe strlen usage in metadata library with safe Rust #1926

Merged
merged 1 commit into from Jul 21, 2022

Conversation

Swatinem
Copy link
Contributor

Removes the unsafe usage of C strlen and unchecked utf8 decoding
from File::str, replacing it with safe (but panicing) Rust code.
This also documents the method at the same time.

This pulls in the memchr crate, and uses almost the same code as the nightly-only CStr::from_bytes_until_nul.

Not sure this subcrate by itself has any tests? So I’m not 100% confident of this change due to the lack of tests?

Copy link
Contributor

@rylev rylev left a comment

Choose a reason for hiding this comment

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

I'm personally in favor of most of the changes here:

  • additional docs
  • checking for utf-8 and panicking otherwise (using from_utf8_unchecked was wrong)

I'm not sure that the dependency on memchar is worth it though. I would say we should accept these changes without the adoption of memchar. We can always change towards using memchar in the future if we encounter issues.

crates/libs/metadata/src/reader/file.rs Outdated Show resolved Hide resolved
Removes the `unsafe` usage of C strlen and unchecked utf8 decoding
from `File::str`, replacing it with safe (but panicing) Rust code.
This also documents the method at the same time.
@kennykerr
Copy link
Collaborator

This dates back to when the metadata reader was primarily used from a proc macro and thus very slow. That's no longer a primary concern so this change looks good.

@kennykerr kennykerr changed the title ref: Replace unsafe strlen usage by safe Rust Replace unsafe strlen usage in metadata library with safe Rust Jul 21, 2022
@kennykerr kennykerr merged commit bc761e3 into microsoft:master Jul 21, 2022
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

Successfully merging this pull request may close these issues.

None yet

4 participants