Skip to content

Commit fd136cc

Browse files
bugfix: wrong argument for pcre2_match.
1 parent af96930 commit fd136cc

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

src/ngx_stream_lua_regex.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,11 +695,11 @@ ngx_stream_lua_ffi_exec_regex(ngx_stream_lua_regex_t *re, int flags,
695695
ngx_pool_t *old_pool;
696696

697697
if (flags & NGX_LUA_RE_MODE_DFA) {
698-
ovecsize = 2;
698+
ovecsize = 1;
699699
re->ncaptures = 0;
700700

701701
} else {
702-
ovecsize = (re->ncaptures + 1) * 3;
702+
ovecsize = re->ncaptures + 1;
703703
}
704704

705705
old_pool = ngx_stream_lua_pcre_malloc_init(NULL);
@@ -717,7 +717,7 @@ ngx_stream_lua_ffi_exec_regex(ngx_stream_lua_regex_t *re, int flags,
717717
}
718718

719719
ngx_regex_match_data_size = ovecsize;
720-
ngx_regex_match_data = pcre2_match_data_create(ovecsize / 3, NULL);
720+
ngx_regex_match_data = pcre2_match_data_create(ovecsize, NULL);
721721

722722
if (ngx_regex_match_data == NULL) {
723723
rc = PCRE2_ERROR_NOMEMORY;
@@ -762,8 +762,8 @@ ngx_stream_lua_ffi_exec_regex(ngx_stream_lua_regex_t *re, int flags,
762762
"n %ui, ovecsize %ui", flags, exec_opts, rc, n, ovecsize);
763763
#endif
764764

765-
if (!(flags & NGX_LUA_RE_MODE_DFA) && n > ovecsize / 3) {
766-
n = ovecsize / 3;
765+
if (n > ovecsize) {
766+
n = ovecsize;
767767
}
768768

769769
for (i = 0; i < n; i++) {
@@ -796,6 +796,21 @@ ngx_stream_lua_ffi_exec_regex(ngx_stream_lua_regex_t *re, int flags,
796796
re->ncaptures = 0;
797797

798798
} else {
799+
/* How pcre_exec() returns captured substrings
800+
* The first two-thirds of the vector is used to pass back captured
801+
* substrings, each substring using a pair of integers. The remaining
802+
* third of the vector is used as workspace by pcre_exec() while
803+
* matching capturing subpatterns, and is not available for passing
804+
* back information. The number passed in ovecsize should always be a
805+
* multiple of three. If it is not, it is rounded down.
806+
*
807+
* When a match is successful, information about captured substrings is
808+
* returned in pairs of integers, starting at the beginning of ovector,
809+
* and continuing up to two-thirds of its length at the most. The first
810+
* element of each pair is set to the byte offset of the first character
811+
* in a substring, and the second is set to the byte offset of the first
812+
* character after the end of a substring.
813+
*/
799814
ovecsize = (re->ncaptures + 1) * 3;
800815
}
801816

t/048-match-dfa.t

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ __DATA__
1919
=== TEST 1: matched with d
2020
--- stream_server_config
2121
content_by_lua_block {
22-
m = ngx.re.match("hello", "(he|hell)", "d")
22+
local m = ngx.re.match("hello", "(he|hell)", "d")
2323
if m then
2424
ngx.say(m[0])
2525
else
@@ -34,7 +34,7 @@ hell
3434
=== TEST 2: matched with d + j
3535
--- stream_server_config
3636
content_by_lua_block {
37-
m = ngx.re.match("hello", "(he|hell)", "jd")
37+
local m = ngx.re.match("hello", "(he|hell)", "jd")
3838
if m then
3939
ngx.say(m[0])
4040
else
@@ -49,7 +49,7 @@ hell
4949
=== TEST 3: not matched with j
5050
--- stream_server_config
5151
content_by_lua_block {
52-
m = ngx.re.match("world", "(he|hell)", "d")
52+
local m = ngx.re.match("world", "(he|hell)", "d")
5353
if m then
5454
ngx.say(m[0])
5555
else
@@ -64,7 +64,7 @@ not matched!
6464
=== TEST 4: matched with do
6565
--- stream_server_config
6666
content_by_lua_block {
67-
m = ngx.re.match("hello", "he|hell", "do")
67+
local m = ngx.re.match("hello", "he|hell", "do")
6868
if m then
6969
ngx.say(m[0])
7070
ngx.say(m[1])
@@ -83,7 +83,7 @@ nil
8383
=== TEST 5: not matched with do
8484
--- stream_server_config
8585
content_by_lua_block {
86-
m = ngx.re.match("world", "([0-9]+)", "do")
86+
local m = ngx.re.match("world", "([0-9]+)", "do")
8787
if m then
8888
ngx.say(m[0])
8989
else
@@ -152,3 +152,26 @@ exec opts: 0
152152
153153
--- no_error_log
154154
[error]
155+
156+
157+
158+
=== TEST 8: matched dfa after nfa
159+
--- stream_server_config
160+
content_by_lua_block {
161+
local m1 = ngx.re.match("hello", "(a)(a)(a)")
162+
if m1 then
163+
ngx.say(m1[0])
164+
else
165+
ngx.say("not matched!")
166+
end
167+
168+
local m2 = ngx.re.match("world", "w", "d")
169+
if m2 then
170+
ngx.say(m2[0])
171+
else
172+
ngx.say("not matched!")
173+
end
174+
}
175+
--- stream_response
176+
not matched!
177+
w

0 commit comments

Comments
 (0)