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

Fix #! (shebang) stripping account space issue #71372

Merged
merged 4 commits into from
Apr 22, 2020

Conversation

ayushmishra2005
Copy link
Contributor

@rust-highfive
Copy link
Collaborator

r? @estebank

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 21, 2020
@JohnTitor
Copy link
Member

Also, it'd be great that if you could also include tests.

@estebank
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Apr 21, 2020

📌 Commit 1b362cd has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 21, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 22, 2020
Rollup of 7 pull requests

Successful merges:

 - rust-lang#70998 (Suggest `-> impl Trait` and `-> Box<dyn Trait>` on fn that doesn't return)
 - rust-lang#71236 (Remove unused rustc_serialize::hex module)
 - rust-lang#71366 (Use assoc int consts3)
 - rust-lang#71372 (Fix #! (shebang) stripping account space issue)
 - rust-lang#71384 (Fix stage0.txt version number comment)
 - rust-lang#71390 (Fix incorrect description of E0690)
 - rust-lang#71399 (Clean up E0554 explanation)

Failed merges:

r? @ghost
@bors bors merged commit 46a8dce into rust-lang:master Apr 22, 2020
return None;
}
Some(input.find('\n').unwrap_or(input.len()))
}

fn remove_whitespace(s: &str) -> String {
s.chars().filter(|c| !c.is_whitespace()).collect()
Copy link
Contributor

Choose a reason for hiding this comment

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

This should use is_whitespace from rustc_lexer instead of the one from libstd.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like in this case

#!
[bad_attribute]

#! is no longer treated as a shebang anymore, which also seems incorrect.

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 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...

Copy link
Member

Choose a reason for hiding this comment

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

Just found #71487, will leave comments there.

Comment on lines +149 to +165
#[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);
}
Copy link
Member

@eddyb eddyb Apr 27, 2020

Choose a reason for hiding this comment

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

There should be UI tests as well.

eddyb added a commit to eddyb/rust that referenced this pull request Apr 28, 2020
…ipping, r=estebank"

This reverts commit 46a8dce, reversing
changes made to f28e387.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Apr 28, 2020
Revert rust-lang#71372 ("Fix #! (shebang) stripping account space issue").

While rust-lang#71372 fixed some of the problems `#!`-stripping had, it introduced others:
* inefficient implementation (`.chars().filter(...).collect()` on the entire input file)
  * this also means the length returned isn't always correct, leading to e.g. rust-lang#71471
* it ignores whitespace anywhere, stripping ` # ! ...` which isn't a valid shebang
  * the definition of "whitespace" it uses includes newlines, which means even `\n#\n!\n...` is stripped as a shebang (and anything matching the regex `\s*#\s*!\s*`, and not followed by `[`, really)
* it's backward-incompatible but didn't go through Crater

Now, rust-lang#71487 is already open and will solve all of these issues. But for running Crater, and just in case rust-lang#71487 takes a bit longer, I decided it's safer to just revert rust-lang#71372.

This will also make rust-lang#71372's diff clearer, as it will start again from the original whitespace-unaware version.

r? @petrochenkov
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 28, 2020
Rollup of 5 pull requests

Successful merges:

 - rust-lang#71311 (On `FnDef` type annotation suggestion, use fn-pointer output)
 - rust-lang#71488 (normalize field projection ty to fix broken MIR issue)
 - rust-lang#71489 (Fix off by one in treat err as bug)
 - rust-lang#71585 (remove obsolete comment)
 - rust-lang#71634 (Revert rust-lang#71372 ("Fix #! (shebang) stripping account space issue").)

Failed merges:

r? @ghost
rcoh added a commit to rcoh/rust that referenced this pull request May 25, 2020
Shebang handling was too agressive in stripping out the first line in cases where it is actually _not_ a shebang, but instead, valid rust (rust-lang#70528). This is a second attempt at resolving this issue (the first attempt was flawed, for, among other reasons, causing an ICE in certain cases (rust-lang#71372, rust-lang#71471).

The behavior is now codified by a number of UI tests, but simply:
For the first line to be a shebang, the following must all be true:
1. The line must start with `#!`
2. The line must contain a non whitespace character after `#!`
3. The next character in the file, ignoring comments & whitespace must not be `[`

I believe this is a strict superset of what we used to allow, so perhaps a crater run is unnecessary, but probably not a terrible idea.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 26, 2020
Fix bug in shebang handling

Shebang handling was too agressive in stripping out the first line in cases where it is actually _not_ a shebang, but instead, valid rust (rust-lang#70528). This is a second attempt at resolving this issue (the first attempt was reverted, for, among other reasons, causing an ICE in certain cases (rust-lang#71372, rust-lang#71471).

The behavior is now codified by a number of UI tests, but simply:
For the first line to be a shebang, the following must all be true:
1. The line must start with `#!`
2. The line must contain a non-whitespace character after `#!`
3. The next character in the file, ignoring comments & whitespace must not be `[`

I believe this is a strict superset of what we used to allow, so perhaps a crater run is unnecessary, but probably not a terrible idea.

Fixes rust-lang#70528
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants