Skip to content

Commit ec804f1

Browse files
authored
Merge pull request #364 from dtolnay/hashes
Reduce max hash in raw strings to 255
2 parents 3b90e7d + dffd53c commit ec804f1

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/parse.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,10 @@ fn raw_string(input: Cursor) -> Result<Cursor, Reject> {
472472
_ => return Err(Reject),
473473
}
474474
}
475+
if n > 255 {
476+
// https://github.com/rust-lang/rust/pull/95251
477+
return Err(Reject);
478+
}
475479
while let Some((i, ch)) = chars.next() {
476480
match ch {
477481
'"' if input.rest[i + 1..].starts_with(&input.rest[..n]) => {

tests/test.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
)]
66

77
use proc_macro2::{Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
8+
use std::iter;
89
use std::panic;
910
use std::str::{self, FromStr};
1011

@@ -118,6 +119,25 @@ fn literal_string() {
118119
#[test]
119120
fn literal_raw_string() {
120121
"r\"\r\n\"".parse::<TokenStream>().unwrap();
122+
123+
fn raw_string_literal_with_hashes(n: usize) -> String {
124+
let mut literal = String::new();
125+
literal.push('r');
126+
literal.extend(iter::repeat('#').take(n));
127+
literal.push('"');
128+
literal.push('"');
129+
literal.extend(iter::repeat('#').take(n));
130+
literal
131+
}
132+
133+
raw_string_literal_with_hashes(255)
134+
.parse::<TokenStream>()
135+
.unwrap();
136+
137+
// https://github.com/rust-lang/rust/pull/95251
138+
raw_string_literal_with_hashes(256)
139+
.parse::<TokenStream>()
140+
.unwrap_err();
121141
}
122142

123143
#[test]

0 commit comments

Comments
 (0)