Skip to content

Commit ad5e7f7

Browse files
committed
bugfix: ngx.re.gmatch might loop infinitely when the pattern matches an empty string. thanks Lance Li and xingxing for tracking this issue down.
1 parent 0b6685c commit ad5e7f7

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/ngx_http_lua_regex.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,11 @@ ngx_http_lua_ngx_re_gmatch_iterator(lua_State *L)
944944
}
945945

946946
offset = cap[1];
947-
if (offset == (ssize_t) subj.len) {
947+
if (offset == cap[0]) {
948+
offset++;
949+
}
950+
951+
if (offset >= (ssize_t) subj.len) {
948952
offset = -1;
949953

950954
if (!(ctx->flags & NGX_LUA_RE_COMPILE_ONCE)) {

t/035-gmatch.t

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,27 @@ sr failed: 500
486486
--- error_log
487487
attempt to use ngx.re.gmatch iterator in a request that did not create it
488488

489+
490+
491+
=== TEST 20: gmatch (empty matched string)
492+
--- config
493+
location /re {
494+
content_by_lua '
495+
for m in ngx.re.gmatch("hello", "a|") do
496+
if m then
497+
ngx.say("matched: [", m[0], "]")
498+
else
499+
ngx.say("not matched: ", m)
500+
end
501+
end
502+
';
503+
}
504+
--- request
505+
GET /re
506+
--- response_body
507+
matched: []
508+
matched: []
509+
matched: []
510+
matched: []
511+
matched: []
512+

0 commit comments

Comments
 (0)