From f0a3490e9797ce1f9677e623693ef2c587051b6e Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 4 Feb 2023 15:21:02 -0800 Subject: [PATCH 1/2] Add raw string literal test cases --- tests/test.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test.rs b/tests/test.rs index 5a8e93fe..0033418e 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -5,6 +5,7 @@ )] use proc_macro2::{Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree}; +use std::iter; use std::panic; use std::str::{self, FromStr}; @@ -118,6 +119,25 @@ fn literal_string() { #[test] fn literal_raw_string() { "r\"\r\n\"".parse::().unwrap(); + + fn raw_string_literal_with_hashes(n: usize) -> String { + let mut literal = String::new(); + literal.push('r'); + literal.extend(iter::repeat('#').take(n)); + literal.push('"'); + literal.push('"'); + literal.extend(iter::repeat('#').take(n)); + literal + } + + raw_string_literal_with_hashes(255) + .parse::() + .unwrap(); + + // https://github.com/rust-lang/rust/pull/95251 + raw_string_literal_with_hashes(256) + .parse::() + .unwrap(); } #[test] From dffd53caa1ae33112be444c0e452ec4f8b046d56 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 4 Feb 2023 15:25:26 -0800 Subject: [PATCH 2/2] Reduce max hash in raw strings to 255 --- src/parse.rs | 4 ++++ tests/test.rs | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/parse.rs b/src/parse.rs index 307e0650..2a87948a 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -472,6 +472,10 @@ fn raw_string(input: Cursor) -> Result { _ => return Err(Reject), } } + if n > 255 { + // https://github.com/rust-lang/rust/pull/95251 + return Err(Reject); + } while let Some((i, ch)) = chars.next() { match ch { '"' if input.rest[i + 1..].starts_with(&input.rest[..n]) => { diff --git a/tests/test.rs b/tests/test.rs index 0033418e..e0af1512 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -137,7 +137,7 @@ fn literal_raw_string() { // https://github.com/rust-lang/rust/pull/95251 raw_string_literal_with_hashes(256) .parse::() - .unwrap(); + .unwrap_err(); } #[test]