Skip to content

Commit 92e22bc

Browse files
committed
bugfix: body_filter_by_lua*: reading ngx.arg[1] after clearing ngx.arg[1] (by assigning nil or "") could lead to segmentation faults. this regression had appeared in v0.9.10. thanks Jason Stangroome for the report in #398.
1 parent f3870f6 commit 92e22bc

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/ngx_http_lua_bodyfilterby.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ ngx_http_lua_body_filter_param_get(lua_State *L)
383383

384384
size = 0;
385385

386+
if (in == NULL) {
387+
/* being a cleared chain on the Lua land */
388+
lua_pushlstring(L, "", 0);
389+
return 1;
390+
}
391+
386392
if (in->next == NULL) {
387393

388394
dd("seen only single buffer");

t/082-body-filter.t

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ log_level('debug');
1111

1212
repeat_each(2);
1313

14-
plan tests => repeat_each() * (blocks() * 3 + 9);
14+
plan tests => repeat_each() * (blocks() * 3 + 13);
1515

1616
#no_diff();
1717
no_long_string();
@@ -754,3 +754,72 @@ probe syscall.writev.return {
754754
[error]
755755
[alert]
756756
757+
758+
759+
=== TEST 24: clear ngx.arg[1] and then read it
760+
--- config
761+
location /t {
762+
echo hello;
763+
echo world;
764+
765+
body_filter_by_lua '
766+
ngx.arg[1] = nil
767+
local data = ngx.arg[1]
768+
print([[data chunk: "]], data, [["]])
769+
770+
ngx.arg[1] = ""
771+
data = ngx.arg[1]
772+
print([[data chunk 2: "]], data, [["]])
773+
';
774+
}
775+
--- request
776+
GET /t
777+
--- response_body
778+
--- log_level: info
779+
--- grep_error_log eval: qr/data chunk(?: \d+)?: [^,]+/
780+
--- grep_error_log_out
781+
data chunk: ""
782+
data chunk 2: ""
783+
data chunk: ""
784+
data chunk 2: ""
785+
data chunk: ""
786+
data chunk 2: ""
787+
--- no_error_log
788+
[error]
789+
[alert]
790+
791+
792+
793+
=== TEST 25: clear ngx.arg[1] and then read ngx.arg[2]
794+
--- config
795+
location /t {
796+
echo hello;
797+
echo world;
798+
799+
body_filter_by_lua '
800+
ngx.arg[1] = nil
801+
local eof = ngx.arg[2]
802+
print([[eof: ]], eof)
803+
804+
ngx.arg[1] = ""
805+
eof = ngx.arg[2]
806+
print([[eof 2: ]], eof)
807+
';
808+
}
809+
--- request
810+
GET /t
811+
--- response_body
812+
--- log_level: info
813+
--- grep_error_log eval: qr/eof(?: \d+)?: [^,]+/
814+
--- grep_error_log_out
815+
eof: false
816+
eof 2: false
817+
eof: false
818+
eof 2: false
819+
eof: true
820+
eof 2: true
821+
822+
--- no_error_log
823+
[error]
824+
[alert]
825+

0 commit comments

Comments
 (0)