From 0633154419955e35470b38aafd49b6f52510333a Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 24 Feb 2022 11:21:45 -0500 Subject: regex: Test if the string is valid before matching --- regex.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/regex.c b/regex.c index 3a14b25..642e357 100644 --- a/regex.c +++ b/regex.c @@ -123,6 +123,14 @@ bool bfs_regexec(struct bfs_regex *regex, const char *str, enum bfs_regexec_flag const unsigned char *ustr = (const unsigned char *)str; const unsigned char *end = ustr + len; + // The docs for onig_{match,search}() say + // + // Do not pass invalid byte string in the regex character encoding. + if (!onigenc_is_valid_mbc_string(onig_get_encoding(regex->impl), ustr, end)) { + *err = 0; + return false; + } + int ret; if (flags & BFS_REGEX_ANCHOR) { ret = onig_match(regex->impl, ustr, end, ustr, NULL, ONIG_OPTION_DEFAULT); -- cgit v1.2.3