Skip to content

Commit 15b693b

Browse files
bugfix: passing the wrong chunkname argument to luaL_loadbuffer. (#2010)
The total length of the chunkname is 60. The format is "=tag(file:line)". So the space left for the tag, file and line is only 56 chars.
1 parent 70a50e6 commit 15b693b

10 files changed

+58
-31
lines changed

src/ngx_http_lua_directive.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
#include "ngx_http_lua_log.h"
3333

3434

35-
#define LJ_CHUNKNAME_MAX_LEN 42
35+
/* the max length is 60, after deducting the fixed four characters "=(:)"
36+
* only 56 left.
37+
*/
38+
#define LJ_CHUNKNAME_MAX_LEN 56
3639

3740

3841
typedef struct ngx_http_lua_block_parser_ctx_s
@@ -1369,7 +1372,7 @@ ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len,
13691372
ngx_str_t *filename;
13701373
u_char *filename_end;
13711374
const char *pre_str = "";
1372-
ngx_uint_t start_line_len;
1375+
ngx_uint_t reserve_len;
13731376

13741377
ngx_http_lua_main_conf_t *lmcf;
13751378

@@ -1385,7 +1388,7 @@ ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len,
13851388
start_line = lmcf->directive_line > 0
13861389
? lmcf->directive_line : cf->conf_file->line;
13871390
p = ngx_snprintf(out, len, "%d", start_line);
1388-
start_line_len = p - out;
1391+
reserve_len = tag_len + p - out;
13891392

13901393
filename = &cf->conf_file->file.name;
13911394
filename_end = filename->data + filename->len;
@@ -1407,8 +1410,8 @@ ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len,
14071410
filename->data, conf_prefix->len) == 0)
14081411
{
14091412
/* files in conf_prefix directory, use the relative path */
1410-
if (filename_end - p + start_line_len > LJ_CHUNKNAME_MAX_LEN) {
1411-
p = filename_end - LJ_CHUNKNAME_MAX_LEN + start_line_len + 3;
1413+
if (filename_end - p + reserve_len > LJ_CHUNKNAME_MAX_LEN) {
1414+
p = filename_end - LJ_CHUNKNAME_MAX_LEN + reserve_len + 3;
14121415
pre_str = "...";
14131416
}
14141417

@@ -1418,11 +1421,11 @@ ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len,
14181421

14191422
p = filename->data;
14201423

1421-
if (filename->len + start_line_len <= LJ_CHUNKNAME_MAX_LEN) {
1424+
if (filename->len + reserve_len <= LJ_CHUNKNAME_MAX_LEN) {
14221425
goto found;
14231426
}
14241427

1425-
p = filename_end - LJ_CHUNKNAME_MAX_LEN + start_line_len + 3;
1428+
p = filename_end - LJ_CHUNKNAME_MAX_LEN + reserve_len + 3;
14261429
pre_str = "...";
14271430

14281431
found:

src/ngx_http_lua_exitworkerby.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,18 @@ ngx_http_lua_exit_worker_by_inline(ngx_log_t *log,
9797
int status;
9898
const char *chunkname;
9999

100-
status = luaL_loadbuffer(L, (char *) lmcf->exit_worker_src.data,
101-
lmcf->exit_worker_src.len, "=exit_worker_by_lua")
102-
|| ngx_http_lua_do_call(log, L);
103-
104100
if (lmcf->exit_worker_chunkname == NULL) {
105-
chunkname = "exit_worker_by_lua";
101+
chunkname = "=exit_worker_by_lua";
106102

107103
} else {
108104
chunkname = (const char *) lmcf->exit_worker_chunkname;
109105
}
110106

111-
return ngx_http_lua_report(log, L, status, chunkname);
107+
status = luaL_loadbuffer(L, (char *) lmcf->exit_worker_src.data,
108+
lmcf->exit_worker_src.len, chunkname)
109+
|| ngx_http_lua_do_call(log, L);
110+
111+
return ngx_http_lua_report(log, L, status, "exit_worker_by_lua");
112112
}
113113

114114

src/ngx_http_lua_initby.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ ngx_http_lua_init_by_inline(ngx_log_t *log, ngx_http_lua_main_conf_t *lmcf,
2121
const char *chunkname;
2222

2323

24-
status = luaL_loadbuffer(L, (char *) lmcf->init_src.data,
25-
lmcf->init_src.len, "=init_by_lua")
26-
|| ngx_http_lua_do_call(log, L);
27-
2824
if (lmcf->init_chunkname == NULL) {
29-
chunkname = "init_by_lua";
25+
chunkname = "=init_by_lua";
3026

3127
} else {
3228
chunkname = (const char *) lmcf->init_chunkname;
3329
}
3430

35-
return ngx_http_lua_report(log, L, status, chunkname);
31+
status = luaL_loadbuffer(L, (char *) lmcf->init_src.data,
32+
lmcf->init_src.len, chunkname)
33+
|| ngx_http_lua_do_call(log, L);
34+
35+
return ngx_http_lua_report(log, L, status, "init_by_lua");
3636
}
3737

3838

src/ngx_http_lua_initworkerby.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,18 +319,18 @@ ngx_http_lua_init_worker_by_inline(ngx_log_t *log,
319319
int status;
320320
const char *chunkname;
321321

322-
status = luaL_loadbuffer(L, (char *) lmcf->init_worker_src.data,
323-
lmcf->init_worker_src.len, "=init_worker_by_lua")
324-
|| ngx_http_lua_do_call(log, L);
325-
326322
if (lmcf->init_worker_chunkname == NULL) {
327-
chunkname = "init_worker_by_lua";
323+
chunkname = "=init_worker_by_lua";
328324

329325
} else {
330326
chunkname = (const char *) lmcf->init_worker_chunkname;
331327
}
332328

333-
return ngx_http_lua_report(log, L, status, chunkname);
329+
status = luaL_loadbuffer(L, (char *) lmcf->init_worker_src.data,
330+
lmcf->init_worker_src.len, chunkname)
331+
|| ngx_http_lua_do_call(log, L);
332+
333+
return ngx_http_lua_report(log, L, status, "init_worker_by_lua");
334334
}
335335

336336

t/041-header-filter.t

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,3 +849,27 @@ GET /lua
849849
failed to load inlined Lua code: header_filter_by_lua(nginx.conf:49):2: unexpected symbol near ''for end''
850850
--- no_error_log
851851
no_such_error
852+
853+
854+
855+
=== TEST 44: syntax error in /tmp/12345678901234567890123456789012345.conf
856+
--- config
857+
location /lua {
858+
content_by_lua_block {
859+
ngx.say("Hello world")
860+
}
861+
862+
include /tmp/12345678901234567890123456789012345.conf;
863+
}
864+
--- user_files
865+
>>> /tmp/12345678901234567890123456789012345.conf
866+
header_filter_by_lua_block {
867+
'for end';
868+
}
869+
--- request
870+
GET /lua
871+
--- ignore_response
872+
--- error_log
873+
failed to load inlined Lua code: header_filter_by_lua(...901234567890123456789012345.conf:1):2: unexpected symbol near ''for end''
874+
--- no_error_log
875+
[alert]

t/086-init-by.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ failed to init
339339
}
340340
--- must_die
341341
--- error_log
342-
=init_by_lua(nginx.conf:25) error: init_by_lua:4: ')' expected (to close '(' at line 3) near 'ngx'
342+
init_by_lua error: init_by_lua(nginx.conf:25):4: ')' expected (to close '(' at line 3) near 'ngx'
343343
--- no_error_log
344344
no_such_error_log
345345

t/089-phase.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,6 @@ GET /lua
198198
ok
199199
--- shutdown_error_log eval
200200
[
201-
qr/exit_worker_by_lua:\d+: exit_worker/,
201+
qr/exit_worker_by_lua\(nginx\.conf:\d+\):\d+: exit_worker/,
202202
qr/exiting now$/,
203203
]

t/091-coroutine.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,9 +1713,9 @@ GET /t
17131713
--- config
17141714
17151715
--- must_die
1716-
--- grep_error_log eval: qr/init_by_lua\(nginx.conf:25\) error: .*? something went wrong/
1716+
--- grep_error_log eval: qr/init_by_lua\(nginx.conf:25\).*? something went wrong/
17171717
--- grep_error_log_out
1718-
init_by_lua(nginx.conf:25) error: init_by_lua:7: init_by_lua:4: something went wrong
1718+
init_by_lua(nginx.conf:25):7: init_by_lua(nginx.conf:25):4: something went wrong
17191719
17201720
17211721

t/124-init-worker.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ start privileged agent process
996996
--- response_body
997997
hello world
998998
--- error_log
999-
=init_worker_by_lua(nginx.conf:25) error: init_worker_by_lua:4: ')' expected (to close '(' at line 3) near 'ngx'
999+
init_worker_by_lua error: init_worker_by_lua(nginx.conf:25):4: ')' expected (to close '(' at line 3) near 'ngx'
10001000
--- no_error_log
10011001
no_such_error_log
10021002

t/162-exit-worker.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ qr/hello from exit worker by lua, process type: worker/,
215215
--- response_body
216216
hello world
217217
--- shutdown_error_log
218-
=exit_worker_by_lua(nginx.conf:25) error: exit_worker_by_lua:4: ')' expected (to close '(' at line 3) near 'ngx'
218+
exit_worker_by_lua error: exit_worker_by_lua(nginx.conf:25):4: ')' expected (to close '(' at line 3) near 'ngx'
219219

220220

221221

0 commit comments

Comments
 (0)