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

Tweak util_libc::open_readonly #434

Merged
merged 2 commits into from
May 29, 2024
Merged

Tweak util_libc::open_readonly #434

merged 2 commits into from
May 29, 2024

Conversation

newpavlov
Copy link
Member

Based on discussion in #432, open_readonly now accepts &[u8] instead of &str for path and asserts that it contains at least one zero byte. This allows to make the function safe and compiler should be able to remove the assert since we use static paths.

/// If `path` does not contain any zeros.
#[inline(always)]
pub fn open_readonly(path: &[u8]) -> Result<libc::c_int, Error> {
assert!(path.iter().any(|&b| b == 0));
Copy link
Contributor

Choose a reason for hiding this comment

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

OK, but since this already returns a Result, we should avoid assert!. Either the compiler is going to optimize away the check completely anyway, or we'd be left with dead code that constructs an Err, or we'd be left with dead code that panics. The possibility of dead code that panics is problematic for some static analysis mechanisms that try to ensure there are no panics in a program.

Copy link
Member

Choose a reason for hiding this comment

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

I think having an assert! here is fine. If this ever triggers, it's an error in our code, not an error in the user's calling environment or input.

For opt-level=1 (https://rust.godbolt.org/z/K3cjrsxqa) or opt-level=s (https://rust.godbolt.org/z/MT9jozE1e) , the assert is optimized away, and on opt-level=0 there's a bunch of dead code that panics, even if the assert is replaced with a branch(https://rust.godbolt.org/z/939exhnKn). Leaving it as-is also makes things more readable/debuggable.

A better approach for this is documenting and testing that our implementation does not panic, I opened #435.

Copy link
Member

@josephlr josephlr left a comment

Choose a reason for hiding this comment

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

Looks good!

/// If `path` does not contain any zeros.
#[inline(always)]
pub fn open_readonly(path: &[u8]) -> Result<libc::c_int, Error> {
assert!(path.iter().any(|&b| b == 0));
Copy link
Member

Choose a reason for hiding this comment

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

I think having an assert! here is fine. If this ever triggers, it's an error in our code, not an error in the user's calling environment or input.

For opt-level=1 (https://rust.godbolt.org/z/K3cjrsxqa) or opt-level=s (https://rust.godbolt.org/z/MT9jozE1e) , the assert is optimized away, and on opt-level=0 there's a bunch of dead code that panics, even if the assert is replaced with a branch(https://rust.godbolt.org/z/939exhnKn). Leaving it as-is also makes things more readable/debuggable.

A better approach for this is documenting and testing that our implementation does not panic, I opened #435.

src/util_libc.rs Show resolved Hide resolved
@newpavlov newpavlov merged commit 0afdaf1 into master May 29, 2024
52 checks passed
@newpavlov newpavlov deleted the tweak_open_readonly branch May 29, 2024 12:29
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.

3 participants