Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2a4c46e

Browse files
committed
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 4dd78bf, 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.
1 parent ace397e commit 2a4c46e

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/backend/regex/regexec.c

+9-4
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,14 @@ zaptreesubs(struct vars * v,
531531
{
532532
if (t->op == '(')
533533
{
534-
assert(t->subno > 0);
535-
v->pmatch[t->subno].rm_so = -1;
536-
v->pmatch[t->subno].rm_eo = -1;
534+
int n = t->subno;
535+
536+
assert(n > 0);
537+
if ((size_t) n < v->nmatch)
538+
{
539+
v->pmatch[n].rm_so = -1;
540+
v->pmatch[n].rm_eo = -1;
541+
}
537542
}
538543

539544
if (t->left != NULL)
@@ -543,7 +548,7 @@ zaptreesubs(struct vars * v,
543548
}
544549

545550
/*
546-
* subset - set any subexpression relevant to a successful subre
551+
* subset - set subexpression match data for a successful subre
547552
*/
548553
static void
549554
subset(struct vars * v,

src/test/regress/expected/regex.out

+19
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,22 @@ select 'abc abc abd' ~ '^(.+)( \1)+$' as f;
7171
f
7272
(1 row)
7373

74+
-- Test some cases that crashed in 9.2beta1 due to pmatch[] array overrun
75+
select substring('asd TO foo' from ' TO (([a-z0-9._]+|"([^"]+|"")+")+)');
76+
substring
77+
-----------
78+
foo
79+
(1 row)
80+
81+
select substring('a' from '((a))+');
82+
substring
83+
-----------
84+
a
85+
(1 row)
86+
87+
select substring('a' from '((a)+)');
88+
substring
89+
-----------
90+
a
91+
(1 row)
92+

src/test/regress/sql/regex.sql

+5
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ select 'abc abc abd' ~ '^(\w+)( \1)+$' as f;
1919
select 'abc abc abc' ~ '^(.+)( \1)+$' as t;
2020
select 'abc abd abc' ~ '^(.+)( \1)+$' as f;
2121
select 'abc abc abd' ~ '^(.+)( \1)+$' as f;
22+
23+
-- Test some cases that crashed in 9.2beta1 due to pmatch[] array overrun
24+
select substring('asd TO foo' from ' TO (([a-z0-9._]+|"([^"]+|"")+")+)');
25+
select substring('a' from '((a))+');
26+
select substring('a' from '((a)+)');

0 commit comments

Comments
 (0)