Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex mutator panics when producing u32 values above char::MAX #62

Open
djuricmilan opened this issue Feb 7, 2024 · 0 comments
Open

Comments

@djuricmilan
Copy link

The following regex rule can produce a u32 value that cannot be converted to a char :

ctx.regex(u'ANY', '.*')

As a consequence, the regex_mutator will panic in append_unicode_range on line 116, because from_u32 will return a None that the code tries to unwrap:

fn append_unicode_range(res: &mut Vec<u8>, scr: &mut RegexScript, cls: ClassUnicodeRange) {
    let mut chr_a_buf = [0; 4];
    let mut chr_b_buf = [0; 4];
    cls.start().encode_utf8(&mut chr_a_buf);
    cls.end().encode_utf8(&mut chr_b_buf);
    let a = u32::from_le_bytes(chr_a_buf);
    let b = u32::from_le_bytes(chr_b_buf);
    let c = scr.get_range(a as usize, (b + 1) as usize) as u32;
    append_char(res, std::char::from_u32(c).unwrap());
}

I suggest capping the value of b+1 to char::MAX to prevent this error, or using char::from_u32_unchecked instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant