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

Made Try{From|Into}Ctx impl on CStr safe. #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Frostie314159
Copy link
Contributor

As discussed, I replaced the unsafe from_bytes_with_nul_unchecked, with from_bytes_until_nul and simplified the writing.

@Frostie314159
Copy link
Contributor Author

I think we discussed this previously, but the library feature we're using, requires an MSRV of 1.69.

Copy link
Owner

@m4b m4b left a comment

Choose a reason for hiding this comment

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

Apologies for the delay, and thanks for adding this; especially the try_into_ctx change is quite nice.

However, the MSRV needs to be updated in order for CI to pass, unfortunately.

Would you mind adding that to this change and then we can merge?

size: 0,
msg: "The input doesn't contain a null byte",
})?;
Ok((cstr, cstr.to_bytes_with_nul().len()))
Copy link
Owner

Choose a reason for hiding this comment

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

the docs say:

Note: This method is currently implemented as a 0-cost cast, but it is planned to alter its definition in the future to perform the length calculation whenever this method is called.

I don't know why they would need to perform the length calculation for this method, ever? So not sure what to think of this, but looking at source it's just an (unsafe, whomp whomp) cast from chars -> u8s, which is fine (and what i'd expect). I wonder if this comment is wrong/outdated.

the only thing I'd say is the previous unsafe version was slightly better since it performed the length calculation (the null byte) while it was also validating, and if for some reason to_bytes_with_nul does a linear scan of the string to get the length (as it says it will) that would be a (very?) minor performance regression from the old. But i'm fine with it if it removes direct unsafe and foists it into the stdlib.

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.

2 participants