From 031586426094f2b535e85e18d2443b792c4ba5db Mon Sep 17 00:00:00 2001 From: Ayush Kumar Mishra Date: Tue, 21 Apr 2020 11:44:00 +0530 Subject: [PATCH 1/4] Fix #! (shebang) stripping account space issue #70528 --- src/librustc_lexer/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_lexer/src/lib.rs b/src/librustc_lexer/src/lib.rs index 5ccfc1b276bfa..0a13783767dff 100644 --- a/src/librustc_lexer/src/lib.rs +++ b/src/librustc_lexer/src/lib.rs @@ -236,7 +236,7 @@ pub enum Base { /// (e.g. "#![deny(missing_docs)]"). pub fn strip_shebang(input: &str) -> Option { debug_assert!(!input.is_empty()); - if !input.starts_with("#!") || input.starts_with("#![") { + if !input.starts_with("#!") || input.starts_with("#![") || input.starts_with("#! [") { return None; } Some(input.find('\n').unwrap_or(input.len())) From ee5a2120f9234ff235777d076666bb8be8386541 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Mishra Date: Tue, 21 Apr 2020 16:48:58 +0530 Subject: [PATCH 2/4] Refactoring and added test-cases #70528 --- src/librustc_lexer/src/lib.rs | 7 ++++++- src/librustc_lexer/src/tests.rs | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/librustc_lexer/src/lib.rs b/src/librustc_lexer/src/lib.rs index 0a13783767dff..c7a803e3029cb 100644 --- a/src/librustc_lexer/src/lib.rs +++ b/src/librustc_lexer/src/lib.rs @@ -236,12 +236,17 @@ pub enum Base { /// (e.g. "#![deny(missing_docs)]"). pub fn strip_shebang(input: &str) -> Option { debug_assert!(!input.is_empty()); - if !input.starts_with("#!") || 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() +} + /// Parses the first token from the provided input string. pub fn first_token(input: &str) -> Token { debug_assert!(!input.is_empty()); diff --git a/src/librustc_lexer/src/tests.rs b/src/librustc_lexer/src/tests.rs index 06fc159fe2516..29491c7851a36 100644 --- a/src/librustc_lexer/src/tests.rs +++ b/src/librustc_lexer/src/tests.rs @@ -145,4 +145,23 @@ 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 = 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 = None; + assert_eq!(expected, actual); + } + } From 365b3cc7818f737860a0d71d7ca180e06bb7475a Mon Sep 17 00:00:00 2001 From: Ayush Kumar Mishra Date: Tue, 21 Apr 2020 17:06:22 +0530 Subject: [PATCH 3/4] Fix formatting issue --- src/librustc_lexer/src/tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/librustc_lexer/src/tests.rs b/src/librustc_lexer/src/tests.rs index 29491c7851a36..065e8f3f646fb 100644 --- a/src/librustc_lexer/src/tests.rs +++ b/src/librustc_lexer/src/tests.rs @@ -163,5 +163,4 @@ mod tests { let expected: Option = None; assert_eq!(expected, actual); } - } From 1b362cd1d5f09f0031b2ce1b161152422a397a67 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Mishra Date: Tue, 21 Apr 2020 22:29:20 +0530 Subject: [PATCH 4/4] Minor refactoring --- src/librustc_lexer/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_lexer/src/lib.rs b/src/librustc_lexer/src/lib.rs index c7a803e3029cb..be85a34bd395a 100644 --- a/src/librustc_lexer/src/lib.rs +++ b/src/librustc_lexer/src/lib.rs @@ -237,7 +237,7 @@ pub enum Base { pub fn strip_shebang(input: &str) -> Option { debug_assert!(!input.is_empty()); let s: &str = &remove_whitespace(input); - if !s.starts_with("#!") || s.starts_with("#![") || s.starts_with("#! [") { + if !s.starts_with("#!") || s.starts_with("#![") { return None; } Some(input.find('\n').unwrap_or(input.len()))