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

New lint: Unix permissions set to a non-octal value #6934

Closed
xTibor opened this issue Mar 18, 2021 · 4 comments · Fixed by #7001
Closed

New lint: Unix permissions set to a non-octal value #6934

xTibor opened this issue Mar 18, 2021 · 4 comments · Fixed by #7001
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy

Comments

@xTibor
Copy link

xTibor commented Mar 18, 2021

What it does

Checks if the following functions expecting Unix permission bits only called with octal literals:

Categories

  • Kind: clippy::correctness

Drawbacks

False positives maybe.

Example

let mut options = OpenOptions::new();
options.mode(440);

This example tries to set the file permissions to 440 (r,r,-) however it ends up as 0o670 (rw,rwx,-) due to the decimal/octal confusion, giving unintended write/execute permissions in the process.

Clippy should suggest rewriting that line as options.mode(0o440);.

@xTibor xTibor added the A-lint Area: New lints label Mar 18, 2021
@giraffate giraffate added the good-first-issue These issues are a good way to get started with Clippy label Mar 19, 2021
@ebobrow
Copy link
Contributor

ebobrow commented Mar 27, 2021

@rustbot claim

@ebobrow
Copy link
Contributor

ebobrow commented Mar 28, 2021

@giraffate Can I get a little help on this one? I need to be able to check if the method mode, for example, is being called on an object of type OpenOptions. Currently, my lint would apply to any object calling any method with the name mode. Sorry, I wasn't sure who to tag here.

@giraffate
Copy link
Contributor

@ebobrow Thanks for working on this!

I think these codes may help you to detect a receiver type of OpenOptions:

impl<'tcx> LateLintPass<'tcx> for OpenOptions {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
if let ExprKind::MethodCall(ref path, _, ref arguments, _) = e.kind {
let obj_ty = cx.typeck_results().expr_ty(&arguments[0]).peel_refs();
if path.ident.name == sym!(open) && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
let mut options = Vec::new();
get_open_options(cx, &arguments[0], &mut options);
check_open_options(cx, &options, e.span);
}
}
}
}

@ebobrow
Copy link
Contributor

ebobrow commented Mar 29, 2021

Thank you so much!

bors added a commit that referenced this issue Mar 30, 2021
Add non_octal_unix_permissions lint

fixes #6934

changelog: add new lint that checks for non-octal values used to set unix file permissions
bors added a commit that referenced this issue Mar 30, 2021
Add non_octal_unix_permissions lint

fixes #6934

changelog: add new lint that checks for non-octal values used to set unix file permissions
bors added a commit that referenced this issue Mar 30, 2021
Add non_octal_unix_permissions lint

fixes #6934

changelog: add new lint that checks for non-octal values used to set unix file permissions
bors added a commit that referenced this issue Mar 30, 2021
Add non_octal_unix_permissions lint

fixes #6934

changelog: add new lint that checks for non-octal values used to set unix file permissions
@bors bors closed this as completed in 4be72b0 Mar 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants