Skip to content

Commit 3eab7b0

Browse files
committed
ensured that proper error message is always thrown when the iterator returned by ngx.re.gmatch is used in the context of another nginx request.
1 parent e73e244 commit 3eab7b0

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/ngx_http_lua_regex.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1721,14 +1721,17 @@ ngx_http_lua_ngx_re_gmatch_cleanup(void *data)
17211721
if (ctx) {
17221722
if (ctx->regex_sd) {
17231723
dd("free study data");
1724-
ngx_http_lua_regex_free_study_data(ctx->request->pool, ctx->regex_sd);
1724+
ngx_http_lua_regex_free_study_data(ctx->request->pool,
1725+
ctx->regex_sd);
17251726
ctx->regex_sd = NULL;
17261727
}
17271728

17281729
if (ctx->cleanup) {
17291730
*ctx->cleanup = NULL;
17301731
ctx->cleanup = NULL;
17311732
}
1733+
1734+
ctx->request = NULL;
17321735
}
17331736

17341737
return;

t/035-gmatch.t

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use Test::Nginx::Socket;
99

1010
repeat_each(2);
1111

12-
plan tests => repeat_each() * (blocks() * 2);
12+
plan tests => repeat_each() * (blocks() * 2 + 1);
13+
14+
our $HtmlDir = html_dir;
1315

1416
#no_diff();
1517
#no_long_string();
@@ -426,3 +428,57 @@ hello
426428
--- response_body
427429
done
428430

431+
432+
433+
=== TEST 19: gmatch iterator used by another request
434+
--- http_config eval
435+
"lua_package_path '$::HtmlDir/?.lua;;';"
436+
--- config
437+
location /main {
438+
content_by_lua '
439+
package.loaded.foo = nil
440+
441+
local res = ngx.location.capture("/t")
442+
if res.status == 200 then
443+
ngx.print(res.body)
444+
else
445+
ngx.say("sr failed: ", res.status)
446+
end
447+
448+
res = ngx.location.capture("/t")
449+
if res.status == 200 then
450+
ngx.print(res.body)
451+
else
452+
ngx.say("sr failed: ", res.status)
453+
end
454+
';
455+
}
456+
457+
location /t {
458+
content_by_lua '
459+
local foo = require "foo"
460+
local m = foo.go()
461+
ngx.say(m and "matched" or "no")
462+
';
463+
}
464+
--- user_files
465+
>>> foo.lua
466+
module("foo", package.seeall)
467+
468+
local it
469+
470+
function go()
471+
if not it then
472+
it = ngx.re.gmatch("hello, world", "[a-z]+")
473+
end
474+
475+
return it()
476+
end
477+
--- request
478+
GET /main
479+
--- response_body
480+
matched
481+
sr failed: 500
482+
--- error_log
483+
attempt to use ngx.re.gmatch iterator in a request that did not create it
484+

0 commit comments

Comments
 (0)