Skip to content

Commit

Permalink
Disallow Unicode literals in character classes when Unicode is disabled.
Browse files Browse the repository at this point in the history
When Unicode mode is disabled, we also disable the use of Unicode literals
in the regular expression, since it can lead to unintuitive behavior. In
this case, Unicode literals in character classes were not disallowed, and
subsequent code filtered them out, which resulted in an empty character
class. The compiler assumes that empty character classes are not allowed,
and so this causes an assert to trigger.

Fixes #250.
  • Loading branch information
BurntSushi committed Jul 9, 2016
1 parent f07b83d commit 81297f0
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions regex-syntax/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,11 @@ impl Parser {
fn parse_class_range(&mut self, class: &mut CharClass, start: char)
-> Result<()> {
if !self.bump_if('-') {
// Make sure we haven't parsed Unicode literals when we shouldn't have.
if !self.flags.unicode {
let _ = try!(self.codepoint_to_one_byte(start));
}

// Not a range, so just push a singleton range.
class.ranges.push(ClassRange::one(start));
return Ok(());
Expand Down Expand Up @@ -651,6 +656,11 @@ impl Parser {
end: end,
}));
}
// Make sure we haven't parsed Unicode literals when we shouldn't have.
if !self.flags.unicode {
let _ = try!(self.codepoint_to_one_byte(start));
let _ = try!(self.codepoint_to_one_byte(end));
}
class.ranges.push(ClassRange::new(start, end));
Ok(())
}
Expand Down Expand Up @@ -2405,6 +2415,13 @@ mod tests {
test_err!(r"☃(?-u:\pL)", 9, ErrorKind::UnicodeNotAllowed, flags);
}

#[test]
fn unicode_class_literal_not_allowed() {
let flags = Flags { allow_bytes: true, .. Flags::default() };
test_err!(r"(?-u)[☃]", 7, ErrorKind::UnicodeNotAllowed, flags);
test_err!(r"(?-u)[☃-☃]", 9, ErrorKind::UnicodeNotAllowed, flags);
}

#[test]
fn unicode_hex_not_allowed() {
let flags = Flags { allow_bytes: true, .. Flags::default() };
Expand Down

0 comments on commit 81297f0

Please sign in to comment.