Skip to content

gen_match: don't crash on bytes patterns containing a dot - #118

Open
rootkiller6788 wants to merge 3 commits into
google:masterfrom
rootkiller6788:fix-gen-match-bytes-any
Open

rootkiller6788 wants to merge 3 commits into
google:masterfrom
rootkiller6788:fix-gen-match-bytes-any

Conversation

@rootkiller6788

Copy link
Copy Markdown

Found this while reading the regex match generator: the . case always built a str, so any bytes pattern with a dot blew up.

Reproduce with the regex hook on:

import atheris, re
atheris.enabled_hooks.add("regex")
re.compile(b"a.c")
# TypeError: can't concat str to bytes

The exception isn't caught, it comes straight out of gen_match while re._compile is hooked, so the user just sees their re.compile call fail. Bytes patterns are pretty common when fuzzing binary formats, and a dot in one isn't unusual.

_ANY now goes through to_correct_type, same as _RANGE and _CATEGORY a few lines below. I added two bytes cases to regex_match_generation_test.py — the three bytes tests that were already there only used literals, so nothing hit this path.

While I was in there I noticed the "generated match did not match" warning wasn't an f-string on its second line, so instead of the pattern it printed the literal text {_pattern!r}. Fixed that in a separate commit (the variable is pattern).

Tested on 3.12.5 and 3.14.3. I couldn't build the native extension locally, so I ran the python-level suites against a stub of the native module: regex_match_generation_test (34) and instrument_bytecode_test (25) pass, ruff check src is clean. The stubbed run can't exercise str_hook_test / regex_coverage_test since those need the real native hooks.

rootkiller6788 added 3 commits September 14, 2026 01:29
_ANY always appended the str "a", so a bytes pattern like b"a.c"
blew up with "can't concat str to bytes" when the regex hook was
enabled. Route it through to_correct_type like the other branches.
The existing bytes tests all used literals only, so nothing caught the
_ANY path. Both new cases fail before the previous commit.
The second half of that message wasn't an f-string, so users got the
literal text {_pattern!r} instead of the regex. Drop the stray
underscore too, the parameter is called pattern.
@google-cla

google-cla Bot commented Nov 12, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@rootkiller6788
rootkiller6788 marked this pull request as ready for review November 18, 2026 13:19
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

Successfully merging this pull request may close these issues.

1 participant