Skip to content

Commit

Permalink
bugfix: access nonexistent fields in the "ngx" table in init_by_lua* …
Browse files Browse the repository at this point in the history
…could lead to the exception "no request object found" because of the overreacting __index metamethod of the "ngx" table.
  • Loading branch information
agentzh committed Jun 23, 2015
1 parent ec3f150 commit 91ff51f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/ngx_http_lua_misc.c
Expand Up @@ -44,12 +44,14 @@ ngx_http_lua_ngx_get(lua_State *L)

r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request object found");
lua_pushnil(L);
return 1;
}

ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no request ctx found");
lua_pushnil(L);
return 1;
}

p = (u_char *) luaL_checklstring(L, -1, &len);
Expand Down
29 changes: 28 additions & 1 deletion t/086-init-by.t
Expand Up @@ -9,7 +9,7 @@ use Test::Nginx::Socket::Lua;

repeat_each(2);

plan tests => repeat_each() * (blocks() * 3 + 2);
plan tests => repeat_each() * (blocks() * 3 + 3);

#no_diff();
#no_long_string();
Expand Down Expand Up @@ -278,3 +278,30 @@ baz = 78
[error]
Failed to resume our co:



=== TEST 11: access a field in the ngx. table
--- http_config
init_by_lua '
print("INIT 1: foo = ", ngx.foo)
ngx.foo = 3
print("INIT 2: foo = ", ngx.foo)
';
--- config
location /t {
echo ok;
}
--- request
GET /t
--- response_body
ok
--- no_error_log
[error]
--- grep_error_log eval: qr/INIT \d+: foo = \S+/
--- grep_error_log_out eval
[
"INIT 1: foo = nil
INIT 2: foo = 3
",
"",
]

0 comments on commit 91ff51f

Please sign in to comment.