Skip to content

Commit

Permalink
Merge pull request #364 from dtolnay/hashes
Browse files Browse the repository at this point in the history
Reduce max hash in raw strings to 255
  • Loading branch information
dtolnay committed Feb 4, 2023
2 parents 3b90e7d + dffd53c commit ec804f1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ fn raw_string(input: Cursor) -> Result<Cursor, Reject> {
_ => 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]) => {
Expand Down
20 changes: 20 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -118,6 +119,25 @@ fn literal_string() {
#[test]
fn literal_raw_string() {
"r\"\r\n\"".parse::<TokenStream>().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::<TokenStream>()
.unwrap();

// https://github.com/rust-lang/rust/pull/95251
raw_string_literal_with_hashes(256)
.parse::<TokenStream>()
.unwrap_err();
}

#[test]
Expand Down

0 comments on commit ec804f1

Please sign in to comment.