-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Fix #! (shebang) stripping account space issue #71372
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -236,12 +236,17 @@ pub enum Base { | |
/// (e.g. "#![deny(missing_docs)]"). | ||
pub fn strip_shebang(input: &str) -> Option<usize> { | ||
debug_assert!(!input.is_empty()); | ||
if !input.starts_with("#!") || input.starts_with("#![") { | ||
let s: &str = &remove_whitespace(input); | ||
if !s.starts_with("#!") || s.starts_with("#![") || s.starts_with("#! [") { | ||
return None; | ||
} | ||
Some(input.find('\n').unwrap_or(input.len())) | ||
} | ||
|
||
fn remove_whitespace(s: &str) -> String { | ||
s.chars().filter(|c| !c.is_whitespace()).collect() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It also doesn't seem reasonable to filter and re-collect all the program text to check for something that almost never happens, or requires checking a couple of characters when it does. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like in this case #!
[bad_attribute]
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think an empty shebang shouldn't be treated as a shebang, especially as it can be part of valid Rust syntax. But I agree with everything else, and I wish this PR had been assigned to you... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just found #71487, will leave comments there. |
||
} | ||
|
||
/// Parses the first token from the provided input string. | ||
pub fn first_token(input: &str) -> Token { | ||
debug_assert!(!input.is_empty()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,4 +145,22 @@ mod tests { | |
}), | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_valid_shebang() { | ||
// https://github.com/rust-lang/rust/issues/70528 | ||
let input = "#!/usr/bin/rustrun"; | ||
let actual = strip_shebang(input); | ||
let expected: Option<usize> = Some(18); | ||
assert_eq!(expected, actual); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_shebang_valid_rust_syntax() { | ||
// https://github.com/rust-lang/rust/issues/70528 | ||
let input = "#! [bad_attribute]"; | ||
let actual = strip_shebang(input); | ||
let expected: Option<usize> = None; | ||
assert_eq!(expected, actual); | ||
} | ||
Comment on lines
+149
to
+165
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be UI tests as well. |
||
} |
Uh oh!
There was an error while loading. Please reload this page.