aboutsummaryrefslogtreecommitdiff
path: root/src/backend/regex/regexec.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2012-05-24 13:56:16 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2012-05-24 13:56:16 -0400
commit2a4c46e0baf2d51117cd4468b28705d01ffcbff9 (patch)
treecf989d8d3b6a7ba0f6572fe35c9bee5c74cf7b20 /src/backend/regex/regexec.c
parentace397e9d24eddc56e7dffa921f506117b602d78 (diff)
downloadpostgresql-2a4c46e0baf2d51117cd4468b28705d01ffcbff9.tar.gz
postgresql-2a4c46e0baf2d51117cd4468b28705d01ffcbff9.zip
Fix array overrun in regex code.
zaptreesubs() was coded to unconditionally reset a capture subre's corresponding pmatch[] entry. However, in regexes without backrefs, that array is caller-supplied and might not have as many entries as the regex has capturing parens. So check the array length and do nothing if there is no corresponding entry, much as subset() does. Failure to check this resulted in a stack clobber in the case reported by Marko Kreen. This bug appears to have been latent in the regex library from the beginning. It was not exposed because find() called dissect() not cdissect(), and the dissect() code path didn't ever call zaptreesubs() (formerly zapmem()). When I unified dissect() and cdissect() in commit 4dd78bf37aa29d04b3f358b08c4a2fa43cf828e7, the problem was exposed. Now that I've seen this, I'm rather suspicious that we might need to back-patch it; but will refrain for now, for lack of evidence that the case can be hit in the previous coding.
Diffstat (limited to 'src/backend/regex/regexec.c')
-rw-r--r--src/backend/regex/regexec.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/backend/regex/regexec.c b/src/backend/regex/regexec.c
index c9c73e978b6..5d7415b3c1a 100644
--- a/src/backend/regex/regexec.c
+++ b/src/backend/regex/regexec.c
@@ -531,9 +531,14 @@ zaptreesubs(struct vars * v,
{
if (t->op == '(')
{
- assert(t->subno > 0);
- v->pmatch[t->subno].rm_so = -1;
- v->pmatch[t->subno].rm_eo = -1;
+ int n = t->subno;
+
+ assert(n > 0);
+ if ((size_t) n < v->nmatch)
+ {
+ v->pmatch[n].rm_so = -1;
+ v->pmatch[n].rm_eo = -1;
+ }
}
if (t->left != NULL)
@@ -543,7 +548,7 @@ zaptreesubs(struct vars * v,
}
/*
- * subset - set any subexpression relevant to a successful subre
+ * subset - set subexpression match data for a successful subre
*/
static void
subset(struct vars * v,