From c01f5f773aafbcccecd26bfe928760370107d7db Mon Sep 17 00:00:00 2001 From: lijunlong Date: Tue, 8 Feb 2022 16:05:14 +0800 Subject: [PATCH 1/3] bugfix: the error message should use the first line rather than the last line of the code block when load lua code block failed. --- src/ngx_http_lua_common.h | 2 ++ src/ngx_http_lua_directive.c | 14 +++++++- t/002-content.t | 69 +++++++++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/ngx_http_lua_common.h b/src/ngx_http_lua_common.h index ed88f0a2f5..65a6415a89 100644 --- a/src/ngx_http_lua_common.h +++ b/src/ngx_http_lua_common.h @@ -276,6 +276,8 @@ struct ngx_http_lua_main_conf_s { of requests */ ngx_uint_t malloc_trim_req_count; + ngx_uint_t directive_line; + #if (nginx_version >= 1011011) /* the following 2 fields are only used by ngx.req.raw_headers() for now */ ngx_buf_t **busy_buf_ptrs; diff --git a/src/ngx_http_lua_directive.c b/src/ngx_http_lua_directive.c index 831132f12d..bfe34b49c8 100644 --- a/src/ngx_http_lua_directive.c +++ b/src/ngx_http_lua_directive.c @@ -1302,6 +1302,9 @@ ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len, { u_char *p, *out; size_t len; + ngx_uint_t start_line; + + ngx_http_lua_main_conf_t *lmcf; len = sizeof("=(:)") - 1 + tag_len + cf->conf_file->file.name.len + NGX_INT64_LEN + 1; @@ -1328,10 +1331,13 @@ ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len, found: + lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); + start_line = lmcf->directive_line ? : cf->conf_file->line; + p = ngx_snprintf(out, len, "=%*s(%*s:%d)%Z", tag_len, tag, cf->conf_file->file.name.data + cf->conf_file->file.name.len - p, - p, cf->conf_file->line); + p, start_line); *chunkname_len = p - out - 1; /* exclude the trailing '\0' byte */ @@ -1343,6 +1349,7 @@ ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len, char * ngx_http_lua_conf_lua_block_parse(ngx_conf_t *cf, ngx_command_t *cmd) { + ngx_http_lua_main_conf_t *lmcf; ngx_http_lua_block_parser_ctx_t ctx; int level = 1; @@ -1376,6 +1383,9 @@ ngx_http_lua_conf_lua_block_parse(ngx_conf_t *cf, ngx_command_t *cmd) ctx.token_len = 0; start_line = cf->conf_file->line; + lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); + lmcf->directive_line = start_line; + dd("init start line: %d", (int) start_line); ctx.start_line = start_line; @@ -1494,6 +1504,8 @@ ngx_http_lua_conf_lua_block_parse(ngx_conf_t *cf, ngx_command_t *cmd) done: + lmcf->directive_line = 0; + if (rc == NGX_ERROR) { return NGX_CONF_ERROR; } diff --git a/t/002-content.t b/t/002-content.t index d6a3600444..cfce71a629 100644 --- a/t/002-content.t +++ b/t/002-content.t @@ -849,4 +849,71 @@ GET /lua --- response_body_like: 500 Internal Server Error --- error_code: 500 --- error_log eval -qr/failed to load inlined Lua code: / +qr/failed to load inlined Lua code: content_by_lua\(nginx.conf:40\)/ + + + +=== TEST 43: syntax error in content_by_lua_block +--- config + location /lua { + + content_by_lua_block { + 'for end'; + } + } +--- request +GET /lua +--- response_body_like: 500 Internal Server Error +--- error_code: 500 +--- error_log eval +qr/failed to load inlined Lua code: content_by_lua\(nginx.conf:41\)/ + + + +=== TEST 44: syntax error in second content_by_lua_block +--- config + location /foo { + content_by_lua_block { + 'for end'; + } + } + + location /lua { + content_by_lua_block { + 'for end'; + } + } +--- request +GET /lua +--- response_body_like: 500 Internal Server Error +--- error_code: 500 +--- error_log eval +qr/failed to load inlined Lua code: content_by_lua\(nginx.conf:46\)/ + + + +=== TEST 45: syntax error in thrid content_by_lua_block +--- config + location /foo { + content_by_lua_block { + 'for end'; + } + } + + location /bar { + content_by_lua_block { + 'for end'; + } + } + + location /lua { + content_by_lua_block { + 'for end'; + } + } +--- request +GET /lua +--- response_body_like: 500 Internal Server Error +--- error_code: 500 +--- error_log eval +qr/failed to load inlined Lua code: content_by_lua\(nginx.conf:52\)/ From dd6262210bd0aebda185fa36083971205e38d7fb Mon Sep 17 00:00:00 2001 From: lijunlong Date: Tue, 8 Feb 2022 18:01:57 +0800 Subject: [PATCH 2/3] tweak testcases num. --- t/002-content.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/002-content.t b/t/002-content.t index cfce71a629..703ba84a26 100644 --- a/t/002-content.t +++ b/t/002-content.t @@ -10,7 +10,7 @@ use Test::Nginx::Socket::Lua; repeat_each(2); #repeat_each(1); -plan tests => repeat_each() * (blocks() * 2 + 24); +plan tests => repeat_each() * (blocks() * 2 + 27); #no_diff(); #no_long_string(); From 5800956cc44192ef7c34a68800fc7bbdcfae1080 Mon Sep 17 00:00:00 2001 From: lijunlong Date: Wed, 9 Feb 2022 08:39:03 +0800 Subject: [PATCH 3/3] do not use gnu extension. --- src/ngx_http_lua_directive.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ngx_http_lua_directive.c b/src/ngx_http_lua_directive.c index bfe34b49c8..4366b4fff6 100644 --- a/src/ngx_http_lua_directive.c +++ b/src/ngx_http_lua_directive.c @@ -1332,7 +1332,8 @@ ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len, found: lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); - start_line = lmcf->directive_line ? : cf->conf_file->line; + start_line = lmcf->directive_line > 0 + ? lmcf->directive_line : cf->conf_file->line; p = ngx_snprintf(out, len, "=%*s(%*s:%d)%Z", tag_len, tag, cf->conf_file->file.name.data