Skip to content

Commit 1244b5c

Browse files
authored
Merge pull request #394 from dtolnay/unicodebytestr
Disallow non-ASCII content inside byte string
2 parents 2c2a98f + 75160e5 commit 1244b5c

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/parse.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,30 @@ fn cooked_string(input: Cursor) -> Result<Cursor, Reject> {
417417
Err(Reject)
418418
}
419419

420+
fn raw_string(input: Cursor) -> Result<Cursor, Reject> {
421+
let (input, delimiter) = delimiter_of_raw_string(input)?;
422+
let mut chars = input.char_indices();
423+
while let Some((i, ch)) = chars.next() {
424+
match ch {
425+
'"' if input.rest[i + 1..].starts_with(delimiter) => {
426+
let rest = input.advance(i + 1 + delimiter.len());
427+
return Ok(literal_suffix(rest));
428+
}
429+
'\r' => match chars.next() {
430+
Some((_, '\n')) => {}
431+
_ => break,
432+
},
433+
_ => {}
434+
}
435+
}
436+
Err(Reject)
437+
}
438+
420439
fn byte_string(input: Cursor) -> Result<Cursor, Reject> {
421440
if let Ok(input) = input.parse("b\"") {
422441
cooked_byte_string(input)
423442
} else if let Ok(input) = input.parse("br") {
424-
raw_string(input)
443+
raw_byte_string(input)
425444
} else {
426445
Err(Reject)
427446
}
@@ -497,7 +516,7 @@ fn delimiter_of_raw_string(input: Cursor) -> PResult<&str> {
497516
Ok((input.advance(n + 1), &input.rest[..n]))
498517
}
499518

500-
fn raw_string(input: Cursor) -> Result<Cursor, Reject> {
519+
fn raw_byte_string(input: Cursor) -> Result<Cursor, Reject> {
501520
let (input, delimiter) = delimiter_of_raw_string(input)?;
502521
let mut chars = input.char_indices();
503522
while let Some((i, ch)) = chars.next() {
@@ -510,7 +529,11 @@ fn raw_string(input: Cursor) -> Result<Cursor, Reject> {
510529
Some((_, '\n')) => {}
511530
_ => break,
512531
},
513-
_ => {}
532+
other => {
533+
if !other.is_ascii() {
534+
break;
535+
}
536+
}
514537
}
515538
}
516539
Err(Reject)

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ fn literal_byte_string() {
163163
"b\"\\\r\n x\"".parse::<TokenStream>().unwrap();
164164
"b\"\\\r\n \rx\"".parse::<TokenStream>().unwrap_err();
165165
"b\"\\\r\n \u{a0}x\"".parse::<TokenStream>().unwrap_err();
166+
"br\"\u{a0}\"".parse::<TokenStream>().unwrap_err();
166167
}
167168

168169
#[test]

0 commit comments

Comments
 (0)