Skip to content

Commit

Permalink
Fix a panic in cooked_byte on utf-8 chars
Browse files Browse the repository at this point in the history
Don't want to slice on the wrong boundary!

Closes #54
  • Loading branch information
alexcrichton committed Jan 16, 2018
1 parent 36931ed commit 8c03033
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/stable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,13 @@ fn cooked_byte(input: Cursor) -> PResult<()> {
};
if ok {
match bytes.next() {
Some((offset, _)) => Ok((input.advance(offset), ())),
Some((offset, _)) => {
if input.chars().as_str().is_char_boundary(offset) {
Ok((input.advance(offset), ()))
} else {
Err(LexError)
}
}
None => Ok((input.advance(input.len()), ())),
}
} else {
Expand Down
9 changes: 9 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extern crate proc_macro2;

use std::str;

use proc_macro2::{Term, Literal, TokenStream};

#[cfg(procmacro2_semver_exempt)]
Expand Down Expand Up @@ -161,3 +163,10 @@ fn span_join() {

assert_eq!(joined1.unwrap().source_file(), source1[0].span.source_file());
}

#[test]
fn no_panic() {
let s = str::from_utf8(b"b\'\xc2\x86 \x00\x00\x00^\"").unwrap();
assert!(s.parse::<proc_macro2::TokenStream>().is_err());
}

0 comments on commit 8c03033

Please sign in to comment.