aboutsummaryrefslogtreecommitdiff
path: root/src/backend/regex/regexec.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2021-09-11 15:19:31 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2021-09-11 15:19:43 -0400
commitb33283cbd336adbf982c21aac1399130c8ffaaa9 (patch)
treecfbda7e957fcaea1e793c31816353f9ab0197555 /src/backend/regex/regexec.c
parentd844cd75a6764a1dd2d8b3410144df9ebf8a0f04 (diff)
downloadpostgresql-b33283cbd336adbf982c21aac1399130c8ffaaa9.tar.gz
postgresql-b33283cbd336adbf982c21aac1399130c8ffaaa9.zip
Make pg_regexec() robust against out-of-range search_start.
If search_start is greater than the length of the string, we should just return REG_NOMATCH immediately. (Note that the equality case should *not* be rejected, since the pattern might be able to match zero characters.) This guards various internal assumptions that the min of a range of string positions is not more than the max. Violation of those assumptions could allow an attempt to fetch string[search_start-1], possibly causing a crash. Jaime Casanova pointed out that this situation is reachable with the new regexp_xxx functions that accept a user-specified start position. I don't believe it's reachable via any in-core call site in v14 and below. However, extensions could possibly call pg_regexec with an out-of-range search_start, so let's back-patch the fix anyway. Discussion: https://postgr.es/m/20210911180357.GA6870@ahch-to
Diffstat (limited to 'src/backend/regex/regexec.c')
-rw-r--r--src/backend/regex/regexec.c2
1 files changed, 2 insertions, 0 deletions
diff --git a/src/backend/regex/regexec.c b/src/backend/regex/regexec.c
index f19fb988199..e72aa8ccfb1 100644
--- a/src/backend/regex/regexec.c
+++ b/src/backend/regex/regexec.c
@@ -200,6 +200,8 @@ pg_regexec(regex_t *re,
return REG_INVARG;
if (re->re_csize != sizeof(chr))
return REG_MIXED;
+ if (search_start > len)
+ return REG_NOMATCH;
/* Initialize locale-dependent support */
pg_set_regex_collation(re->re_collation);