Skip to content

Commit 8c03033

Browse files
committed
Fix a panic in cooked_byte on utf-8 chars
Don't want to slice on the wrong boundary! Closes #54
1 parent 36931ed commit 8c03033

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/stable.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,13 @@ fn cooked_byte(input: Cursor) -> PResult<()> {
904904
};
905905
if ok {
906906
match bytes.next() {
907-
Some((offset, _)) => Ok((input.advance(offset), ())),
907+
Some((offset, _)) => {
908+
if input.chars().as_str().is_char_boundary(offset) {
909+
Ok((input.advance(offset), ()))
910+
} else {
911+
Err(LexError)
912+
}
913+
}
908914
None => Ok((input.advance(input.len()), ())),
909915
}
910916
} else {

tests/test.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
extern crate proc_macro2;
22

3+
use std::str;
4+
35
use proc_macro2::{Term, Literal, TokenStream};
46

57
#[cfg(procmacro2_semver_exempt)]
@@ -161,3 +163,10 @@ fn span_join() {
161163

162164
assert_eq!(joined1.unwrap().source_file(), source1[0].span.source_file());
163165
}
166+
167+
#[test]
168+
fn no_panic() {
169+
let s = str::from_utf8(b"b\'\xc2\x86 \x00\x00\x00^\"").unwrap();
170+
assert!(s.parse::<proc_macro2::TokenStream>().is_err());
171+
}
172+

0 commit comments

Comments
 (0)