Skip to content

Commit 5cf9710

Browse files
committed
Merge branch 'PHP-7.4'
* PHP-7.4: Fixed bug #79257
2 parents b79af3d + 3a51530 commit 5cf9710

File tree

2 files changed

+235
-5
lines changed

2 files changed

+235
-5
lines changed

ext/pcre/php_pcre.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,20 @@ static inline void populate_match_value(
974974
}
975975
}
976976

977+
static inline void add_named(
978+
zval *subpats, zend_string *name, zval *val, zend_bool unmatched) {
979+
/* If the DUPNAMES option is used, multiple subpatterns might have the same name.
980+
* In this case we want to preserve the one that actually has a value. */
981+
if (!unmatched) {
982+
zend_hash_update(Z_ARRVAL_P(subpats), name, val);
983+
} else {
984+
if (!zend_hash_add(Z_ARRVAL_P(subpats), name, val)) {
985+
return;
986+
}
987+
}
988+
Z_TRY_ADDREF_P(val);
989+
}
990+
977991
/* {{{ add_offset_pair */
978992
static inline void add_offset_pair(
979993
zval *result, const char *subject, PCRE2_SIZE start_offset, PCRE2_SIZE end_offset,
@@ -1002,8 +1016,7 @@ static inline void add_offset_pair(
10021016
}
10031017

10041018
if (name) {
1005-
Z_ADDREF(match_pair);
1006-
zend_hash_update(Z_ARRVAL_P(result), name, &match_pair);
1019+
add_named(result, name, &match_pair, start_offset == PCRE2_UNSET);
10071020
}
10081021
zend_hash_next_index_insert(Z_ARRVAL_P(result), &match_pair);
10091022
}
@@ -1033,16 +1046,15 @@ static void populate_subpat_array(
10331046
populate_match_value(
10341047
&val, subject, offsets[2*i], offsets[2*i+1], unmatched_as_null);
10351048
if (subpat_names[i]) {
1036-
Z_TRY_ADDREF(val);
1037-
zend_hash_update(Z_ARRVAL_P(subpats), subpat_names[i], &val);
1049+
add_named(subpats, subpat_names[i], &val, offsets[2*i] == PCRE2_UNSET);
10381050
}
10391051
zend_hash_next_index_insert(Z_ARRVAL_P(subpats), &val);
10401052
}
10411053
if (unmatched_as_null) {
10421054
for (i = count; i < num_subpats; i++) {
10431055
ZVAL_NULL(&val);
10441056
if (subpat_names[i]) {
1045-
zend_hash_update(Z_ARRVAL_P(subpats), subpat_names[i], &val);
1057+
zend_hash_add(Z_ARRVAL_P(subpats), subpat_names[i], &val);
10461058
}
10471059
zend_hash_next_index_insert(Z_ARRVAL_P(subpats), &val);
10481060
}

ext/pcre/tests/bug79257.phpt

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
--TEST--
2+
Bug #79257: Duplicate named groups (?J) prefer last alternative even if not matched
3+
--FILE--
4+
<?php
5+
6+
preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))/', 'foo', $matches);
7+
var_dump($matches);
8+
preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))/', 'foo', $matches,
9+
PREG_UNMATCHED_AS_NULL);
10+
var_dump($matches);
11+
preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))/', 'foo', $matches,
12+
PREG_OFFSET_CAPTURE);
13+
var_dump($matches);
14+
preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))/', 'foo', $matches,
15+
PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE);
16+
var_dump($matches);
17+
18+
preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))(?<h>baz)/', 'foobaz', $matches);
19+
var_dump($matches);
20+
preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))(?<h>baz)/', 'foobaz', $matches,
21+
PREG_UNMATCHED_AS_NULL);
22+
var_dump($matches);
23+
preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))(?<h>baz)/', 'foobaz', $matches,
24+
PREG_OFFSET_CAPTURE);
25+
var_dump($matches);
26+
preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))(?<h>baz)/', 'foobaz', $matches,
27+
PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE);
28+
var_dump($matches);
29+
30+
?>
31+
--EXPECT--
32+
array(3) {
33+
[0]=>
34+
string(3) "foo"
35+
["g"]=>
36+
string(3) "foo"
37+
[1]=>
38+
string(3) "foo"
39+
}
40+
array(4) {
41+
[0]=>
42+
string(3) "foo"
43+
["g"]=>
44+
string(3) "foo"
45+
[1]=>
46+
string(3) "foo"
47+
[2]=>
48+
NULL
49+
}
50+
array(3) {
51+
[0]=>
52+
array(2) {
53+
[0]=>
54+
string(3) "foo"
55+
[1]=>
56+
int(0)
57+
}
58+
["g"]=>
59+
array(2) {
60+
[0]=>
61+
string(3) "foo"
62+
[1]=>
63+
int(0)
64+
}
65+
[1]=>
66+
array(2) {
67+
[0]=>
68+
string(3) "foo"
69+
[1]=>
70+
int(0)
71+
}
72+
}
73+
array(4) {
74+
[0]=>
75+
array(2) {
76+
[0]=>
77+
string(3) "foo"
78+
[1]=>
79+
int(0)
80+
}
81+
["g"]=>
82+
array(2) {
83+
[0]=>
84+
string(3) "foo"
85+
[1]=>
86+
int(0)
87+
}
88+
[1]=>
89+
array(2) {
90+
[0]=>
91+
string(3) "foo"
92+
[1]=>
93+
int(0)
94+
}
95+
[2]=>
96+
array(2) {
97+
[0]=>
98+
NULL
99+
[1]=>
100+
int(-1)
101+
}
102+
}
103+
array(6) {
104+
[0]=>
105+
string(6) "foobaz"
106+
["g"]=>
107+
string(3) "foo"
108+
[1]=>
109+
string(3) "foo"
110+
[2]=>
111+
string(0) ""
112+
["h"]=>
113+
string(3) "baz"
114+
[3]=>
115+
string(3) "baz"
116+
}
117+
array(6) {
118+
[0]=>
119+
string(6) "foobaz"
120+
["g"]=>
121+
string(3) "foo"
122+
[1]=>
123+
string(3) "foo"
124+
[2]=>
125+
NULL
126+
["h"]=>
127+
string(3) "baz"
128+
[3]=>
129+
string(3) "baz"
130+
}
131+
array(6) {
132+
[0]=>
133+
array(2) {
134+
[0]=>
135+
string(6) "foobaz"
136+
[1]=>
137+
int(0)
138+
}
139+
["g"]=>
140+
array(2) {
141+
[0]=>
142+
string(3) "foo"
143+
[1]=>
144+
int(0)
145+
}
146+
[1]=>
147+
array(2) {
148+
[0]=>
149+
string(3) "foo"
150+
[1]=>
151+
int(0)
152+
}
153+
[2]=>
154+
array(2) {
155+
[0]=>
156+
string(0) ""
157+
[1]=>
158+
int(-1)
159+
}
160+
["h"]=>
161+
array(2) {
162+
[0]=>
163+
string(3) "baz"
164+
[1]=>
165+
int(3)
166+
}
167+
[3]=>
168+
array(2) {
169+
[0]=>
170+
string(3) "baz"
171+
[1]=>
172+
int(3)
173+
}
174+
}
175+
array(6) {
176+
[0]=>
177+
array(2) {
178+
[0]=>
179+
string(6) "foobaz"
180+
[1]=>
181+
int(0)
182+
}
183+
["g"]=>
184+
array(2) {
185+
[0]=>
186+
string(3) "foo"
187+
[1]=>
188+
int(0)
189+
}
190+
[1]=>
191+
array(2) {
192+
[0]=>
193+
string(3) "foo"
194+
[1]=>
195+
int(0)
196+
}
197+
[2]=>
198+
array(2) {
199+
[0]=>
200+
NULL
201+
[1]=>
202+
int(-1)
203+
}
204+
["h"]=>
205+
array(2) {
206+
[0]=>
207+
string(3) "baz"
208+
[1]=>
209+
int(3)
210+
}
211+
[3]=>
212+
array(2) {
213+
[0]=>
214+
string(3) "baz"
215+
[1]=>
216+
int(3)
217+
}
218+
}

0 commit comments

Comments
 (0)