summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2022-02-24 11:21:45 -0500
committerTavian Barnes <tavianator@tavianator.com>2022-02-24 11:21:45 -0500
commit0633154419955e35470b38aafd49b6f52510333a (patch)
tree67788d8f14938d3073920ca4d548024903b425bf
parent81bc52baa51f7615f11338b80c98dff86c1578f7 (diff)
downloadbfs-0633154419955e35470b38aafd49b6f52510333a.tar.xz
regex: Test if the string is valid before matching
-rw-r--r--regex.c8
1 files changed, 8 insertions, 0 deletions
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);