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

Manual parser for umask values. #70

Merged
merged 1 commit into from
Sep 25, 2024
Merged

Conversation

ayosec
Copy link
Contributor

@ayosec ayosec commented Sep 24, 2024

This patch changes the implementation of get_umask_procfs to avoid the dependency on the regex crate.

The original expression ^Umask:\s*(0[0-7]+)$ is implemented as:

if let Some(umask) = line.strip_prefix("Umask:") {
    return Ok(mode_t::from_str_radix(umask.trim(), 8).ok());
}

The previous code was using expect to get the result from from_str_radix. Now it just converts the Result from from_str_radix to an Option. The logic is different, but the behaviour is the same:

  • If the value is correct (like Umask: 0022), both Ok(Some(from_str_radix().expect())) and Ok(from_str_radix().ok()) returns the same value.
  • If the value is unexpected (like Umask: 0999),
    • The previous code will ignore the line (it does not match the regular expression), and will return Ok(None) after reading the /proc/self/status file.
    • The new code will try to parse 0999 as an octal. It will return an error, which is then converted to None by ok().

Build Improvements

After removing the regex crate, the size of the library and the compilation time improve significantly.

Before Now
libpathrs.so size 2.2 M 608 K
Build time 36 s 16 s
  • Before the patch:

    $ hyperfine -w 1 -r 4 -p 'cargo clean || :' 'make release'
    Benchmark 1: make release
      Time (mean ± σ):     36.602 s ±  0.050 s    [User: 61.361 s, System: 4.345 s]
      Range (min … max):   36.552 s … 36.669 s    4 runs
    
    $ ls -sh target/release/libpathrs.so
    2.2M target/release/libpathrs.so
  • After the patch:

    $ hyperfine -w 1 -r 4 -p 'cargo clean || :' 'make release'
    Benchmark 1: make release
      Time (mean ± σ):     16.919 s ±  0.129 s    [User: 26.672 s, System: 2.974 s]
      Range (min … max):   16.744 s … 17.035 s    4 runs
    
    $ ls -sh target/release/libpathrs.so
    608K target/release/libpathrs.so

The line from /proc/self/status is now parsed with str::strip_prefix() and
str::trim().

This change allows to remove the dependency on the regex crate, so it reduces
the size of the .so file, and the build time.

Signed-off-by: Ayose <[email protected]>
@cyphar
Copy link
Member

cyphar commented Sep 25, 2024

This is nicer, though it seems we might remove this code entirely because it turns out checking the mode this strictly is kind of buggy.

See cyphar/filepath-securejoin#29 and opencontainers/runc#4408.

@cyphar cyphar added this to the 0.1.1 milestone Sep 25, 2024
@ayosec
Copy link
Contributor Author

ayosec commented Sep 25, 2024

This is nicer, though it seems we might remove this code entirely because it turns out checking the mode this strictly is kind of buggy.

No problem!

Feel free to close the pull-request if you plan to remove the umask parser before the next release.

Copy link
Member

@cyphar cyphar left a comment

Choose a reason for hiding this comment

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

Actually, let's just merge this as-is since it improves things. We can remove the code later.

@cyphar cyphar merged commit ca0671c into openSUSE:main Sep 25, 2024
15 checks passed
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