Skip to content

Commit a5c924b

Browse files
authored
bugfix: correct offset vector memory allocation size for PCRE2.
1 parent ff9dea8 commit a5c924b

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/ngx_stream_lua_regex.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,11 @@ ngx_stream_lua_ffi_compile_regex(const unsigned char *pat, size_t pat_len,
598598
re_comp.captures = 0;
599599

600600
} else {
601+
#if (NGX_PCRE2)
602+
ovecsize = (re_comp.captures + 1) * 2;
603+
#else
601604
ovecsize = (re_comp.captures + 1) * 3;
605+
#endif
602606
}
603607

604608
dd("allocating cap with size: %d", (int) ovecsize);
@@ -691,21 +695,21 @@ ngx_stream_lua_ffi_exec_regex(ngx_stream_lua_regex_t *re, int flags,
691695
{
692696
int rc, exec_opts = 0;
693697
size_t *ov;
694-
ngx_uint_t ovecsize, n, i;
698+
ngx_uint_t ovecpair, n, i;
695699
ngx_pool_t *old_pool;
696700

697701
if (flags & NGX_LUA_RE_MODE_DFA) {
698-
ovecsize = 1;
702+
ovecpair = 1;
699703
re->ncaptures = 0;
700704

701705
} else {
702-
ovecsize = re->ncaptures + 1;
706+
ovecpair = re->ncaptures + 1;
703707
}
704708

705709
old_pool = ngx_stream_lua_pcre_malloc_init(NULL);
706710

707711
if (ngx_regex_match_data == NULL
708-
|| ovecsize > ngx_regex_match_data_size)
712+
|| ovecpair > ngx_regex_match_data_size)
709713
{
710714
/*
711715
* Allocate a match data if not yet allocated or smaller than
@@ -716,8 +720,8 @@ ngx_stream_lua_ffi_exec_regex(ngx_stream_lua_regex_t *re, int flags,
716720
pcre2_match_data_free(ngx_regex_match_data);
717721
}
718722

719-
ngx_regex_match_data_size = ovecsize;
720-
ngx_regex_match_data = pcre2_match_data_create(ovecsize, NULL);
723+
ngx_regex_match_data_size = ovecpair;
724+
ngx_regex_match_data = pcre2_match_data_create(ovecpair, NULL);
721725

722726
if (ngx_regex_match_data == NULL) {
723727
rc = PCRE2_ERROR_NOMEMORY;
@@ -747,7 +751,7 @@ ngx_stream_lua_ffi_exec_regex(ngx_stream_lua_regex_t *re, int flags,
747751
#if (NGX_DEBUG)
748752
ngx_log_debug4(NGX_LOG_DEBUG_STREAM, ngx_cycle->log, 0,
749753
"pcre2_match failed: flags 0x%05Xd, options 0x%08Xd, rc %d, "
750-
"ovecsize %ui", flags, exec_opts, rc, ovecsize);
754+
"ovecpair %ui", flags, exec_opts, rc, ovecpair);
751755
#endif
752756

753757
goto failed;
@@ -759,11 +763,11 @@ ngx_stream_lua_ffi_exec_regex(ngx_stream_lua_regex_t *re, int flags,
759763
#if (NGX_DEBUG)
760764
ngx_log_debug5(NGX_LOG_DEBUG_STREAM, ngx_cycle->log, 0,
761765
"pcre2_match: flags 0x%05Xd, options 0x%08Xd, rc %d, "
762-
"n %ui, ovecsize %ui", flags, exec_opts, rc, n, ovecsize);
766+
"n %ui, ovecpair %ui", flags, exec_opts, rc, n, ovecpair);
763767
#endif
764768

765-
if (n > ovecsize) {
766-
n = ovecsize;
769+
if (n > ovecpair) {
770+
n = ovecpair;
767771
}
768772

769773
for (i = 0; i < n; i++) {

0 commit comments

Comments
 (0)