Skip to content

Commit 11aa463

Browse files
spacewanderagentzh
authored andcommitted
bugfix: avoided sharing the same code object for identical Lua inlined code chunks in different phases due to chunk name conflicts.
thanks yandongxiao for the report in #1232. Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent e343b3a commit 11aa463

File tree

6 files changed

+182
-18
lines changed

6 files changed

+182
-18
lines changed

src/ngx_http_lua_balancer.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,15 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
173173

174174
lscf->balancer.src = value[1];
175175

176-
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
176+
p = ngx_palloc(cf->pool,
177+
sizeof("balancer_by_lua") + NGX_HTTP_LUA_INLINE_KEY_LEN);
177178
if (p == NULL) {
178179
return NGX_CONF_ERROR;
179180
}
180181

181182
lscf->balancer.src_key = p;
182183

184+
p = ngx_copy(p, "balancer_by_lua", sizeof("balancer_by_lua") - 1);
183185
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
184186
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
185187
*p = '\0';

src/ngx_http_lua_directive.c

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static ngx_int_t ngx_http_lua_set_by_lua_init(ngx_http_request_t *r);
4343
#endif
4444

4545
static u_char *ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag,
46-
size_t tag_len);
46+
size_t tag_len, size_t *chunkname_len);
4747
static ngx_int_t ngx_http_lua_conf_read_lua_token(ngx_conf_t *cf,
4848
ngx_http_lua_block_parser_ctx_t *ctx);
4949
static u_char *ngx_http_lua_strlstrn(u_char *s1, u_char *last, u_char *s2,
@@ -271,13 +271,15 @@ ngx_http_lua_set_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
271271

272272
filter_data->size = filter.size;
273273

274-
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
274+
p = ngx_palloc(cf->pool,
275+
sizeof("set_by_lua") + NGX_HTTP_LUA_INLINE_KEY_LEN);
275276
if (p == NULL) {
276277
return NGX_CONF_ERROR;
277278
}
278279

279280
filter_data->key = p;
280281

282+
p = ngx_copy(p, "set_by_lua", sizeof("set_by_lua") - 1);
281283
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
282284
p = ngx_http_lua_digest_hex(p, value[2].data, value[2].len);
283285
*p = '\0';
@@ -448,6 +450,7 @@ ngx_http_lua_rewrite_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd,
448450
char *
449451
ngx_http_lua_rewrite_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
450452
{
453+
size_t chunkname_len;
451454
u_char *p, *chunkname;
452455
ngx_str_t *value;
453456
ngx_http_lua_main_conf_t *lmcf;
@@ -482,7 +485,8 @@ ngx_http_lua_rewrite_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
482485

483486
if (cmd->post == ngx_http_lua_rewrite_handler_inline) {
484487
chunkname = ngx_http_lua_gen_chunk_name(cf, "rewrite_by_lua",
485-
sizeof("rewrite_by_lua") - 1);
488+
sizeof("rewrite_by_lua") - 1,
489+
&chunkname_len);
486490
if (chunkname == NULL) {
487491
return NGX_CONF_ERROR;
488492
}
@@ -493,13 +497,15 @@ ngx_http_lua_rewrite_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
493497

494498
llcf->rewrite_src.value = value[1];
495499

496-
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
500+
p = ngx_palloc(cf->pool,
501+
chunkname_len + NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
497502
if (p == NULL) {
498503
return NGX_CONF_ERROR;
499504
}
500505

501506
llcf->rewrite_src_key = p;
502507

508+
p = ngx_copy(p, chunkname, chunkname_len);
503509
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
504510
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
505511
*p = '\0';
@@ -562,6 +568,7 @@ ngx_http_lua_access_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd,
562568
char *
563569
ngx_http_lua_access_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
564570
{
571+
size_t chunkname_len;
565572
u_char *p, *chunkname;
566573
ngx_str_t *value;
567574
ngx_http_lua_main_conf_t *lmcf;
@@ -592,7 +599,8 @@ ngx_http_lua_access_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
592599

593600
if (cmd->post == ngx_http_lua_access_handler_inline) {
594601
chunkname = ngx_http_lua_gen_chunk_name(cf, "access_by_lua",
595-
sizeof("access_by_lua") - 1);
602+
sizeof("access_by_lua") - 1,
603+
&chunkname_len);
596604
if (chunkname == NULL) {
597605
return NGX_CONF_ERROR;
598606
}
@@ -603,13 +611,15 @@ ngx_http_lua_access_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
603611

604612
llcf->access_src.value = value[1];
605613

606-
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
614+
p = ngx_palloc(cf->pool,
615+
chunkname_len + NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
607616
if (p == NULL) {
608617
return NGX_CONF_ERROR;
609618
}
610619

611620
llcf->access_src_key = p;
612621

622+
p = ngx_copy(p, chunkname, chunkname_len);
613623
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
614624
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
615625
*p = '\0';
@@ -672,6 +682,7 @@ ngx_http_lua_content_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd,
672682
char *
673683
ngx_http_lua_content_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
674684
{
685+
size_t chunkname_len;
675686
u_char *p;
676687
u_char *chunkname;
677688
ngx_str_t *value;
@@ -706,7 +717,8 @@ ngx_http_lua_content_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
706717

707718
if (cmd->post == ngx_http_lua_content_handler_inline) {
708719
chunkname = ngx_http_lua_gen_chunk_name(cf, "content_by_lua",
709-
sizeof("content_by_lua") - 1);
720+
sizeof("content_by_lua") - 1,
721+
&chunkname_len);
710722
if (chunkname == NULL) {
711723
return NGX_CONF_ERROR;
712724
}
@@ -719,13 +731,15 @@ ngx_http_lua_content_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
719731

720732
llcf->content_src.value = value[1];
721733

722-
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
734+
p = ngx_palloc(cf->pool,
735+
chunkname_len + NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
723736
if (p == NULL) {
724737
return NGX_CONF_ERROR;
725738
}
726739

727740
llcf->content_src_key = p;
728741

742+
p = ngx_copy(p, chunkname, chunkname_len);
729743
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
730744
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
731745
*p = '\0';
@@ -795,6 +809,7 @@ ngx_http_lua_log_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd,
795809
char *
796810
ngx_http_lua_log_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
797811
{
812+
size_t chunkname_len;
798813
u_char *p, *chunkname;
799814
ngx_str_t *value;
800815
ngx_http_lua_main_conf_t *lmcf;
@@ -825,7 +840,8 @@ ngx_http_lua_log_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
825840

826841
if (cmd->post == ngx_http_lua_log_handler_inline) {
827842
chunkname = ngx_http_lua_gen_chunk_name(cf, "log_by_lua",
828-
sizeof("log_by_lua") - 1);
843+
sizeof("log_by_lua") - 1,
844+
&chunkname_len);
829845
if (chunkname == NULL) {
830846
return NGX_CONF_ERROR;
831847
}
@@ -836,13 +852,15 @@ ngx_http_lua_log_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
836852

837853
llcf->log_src.value = value[1];
838854

839-
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
855+
p = ngx_palloc(cf->pool,
856+
chunkname_len + NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
840857
if (p == NULL) {
841858
return NGX_CONF_ERROR;
842859
}
843860

844861
llcf->log_src_key = p;
845862

863+
p = ngx_copy(p, chunkname, chunkname_len);
846864
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
847865
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
848866
*p = '\0';
@@ -936,13 +954,17 @@ ngx_http_lua_header_filter_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
936954
/* Don't eval nginx variables for inline lua code */
937955
llcf->header_filter_src.value = value[1];
938956

939-
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
957+
p = ngx_palloc(cf->pool,
958+
sizeof("header_filter_by_lua") +
959+
NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
940960
if (p == NULL) {
941961
return NGX_CONF_ERROR;
942962
}
943963

944964
llcf->header_filter_src_key = p;
945965

966+
p = ngx_copy(p, "header_filter_by_lua",
967+
sizeof("header_filter_by_lua") - 1);
946968
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
947969
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
948970
*p = '\0';
@@ -1036,13 +1058,16 @@ ngx_http_lua_body_filter_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
10361058
/* Don't eval nginx variables for inline lua code */
10371059
llcf->body_filter_src.value = value[1];
10381060

1039-
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
1061+
p = ngx_palloc(cf->pool,
1062+
sizeof("body_filter_by_lua") +
1063+
NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
10401064
if (p == NULL) {
10411065
return NGX_CONF_ERROR;
10421066
}
10431067

10441068
llcf->body_filter_src_key = p;
10451069

1070+
p = ngx_copy(p, "body_filter_by_lua", sizeof("body_filter_by_lua") - 1);
10461071
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
10471072
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
10481073
*p = '\0';
@@ -1248,7 +1273,8 @@ ngx_http_lua_set_by_lua_init(ngx_http_request_t *r)
12481273

12491274

12501275
static u_char *
1251-
ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len)
1276+
ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len,
1277+
size_t *chunkname_len)
12521278
{
12531279
u_char *p, *out;
12541280
size_t len;
@@ -1282,6 +1308,7 @@ ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len)
12821308
tag_len, tag, cf->conf_file->file.name.data
12831309
+ cf->conf_file->file.name.len - p,
12841310
p, cf->conf_file->line);
1311+
*chunkname_len = len;
12851312

12861313
return out;
12871314
}

src/ngx_http_lua_ssl_certby.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,17 @@ ngx_http_lua_ssl_cert_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
163163

164164
lscf->srv.ssl_cert_src = value[1];
165165

166-
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
166+
p = ngx_palloc(cf->pool,
167+
sizeof("ssl_certificate_by_lua") +
168+
NGX_HTTP_LUA_INLINE_KEY_LEN);
167169
if (p == NULL) {
168170
return NGX_CONF_ERROR;
169171
}
170172

171173
lscf->srv.ssl_cert_src_key = p;
172174

175+
p = ngx_copy(p, "ssl_certificate_by_lua",
176+
sizeof("ssl_certificate_by_lua") - 1);
173177
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
174178
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
175179
*p = '\0';

src/ngx_http_lua_ssl_session_fetchby.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,17 @@ ngx_http_lua_ssl_sess_fetch_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
153153

154154
lscf->srv.ssl_sess_fetch_src = value[1];
155155

156-
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
156+
p = ngx_palloc(cf->pool,
157+
sizeof("ssl_session_fetch_by_lua") +
158+
NGX_HTTP_LUA_INLINE_KEY_LEN);
157159
if (p == NULL) {
158160
return NGX_CONF_ERROR;
159161
}
160162

161163
lscf->srv.ssl_sess_fetch_src_key = p;
162164

165+
p = ngx_copy(p, "ssl_session_fetch_by_lua",
166+
sizeof("ssl_session_fetch_by_lua") - 1);
163167
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
164168
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
165169
*p = '\0';

src/ngx_http_lua_ssl_session_storeby.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,17 @@ ngx_http_lua_ssl_sess_store_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
151151

152152
lscf->srv.ssl_sess_store_src = value[1];
153153

154-
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
154+
p = ngx_palloc(cf->pool,
155+
sizeof("ssl_session_store_by_lua") +
156+
NGX_HTTP_LUA_INLINE_KEY_LEN);
155157
if (p == NULL) {
156158
return NGX_CONF_ERROR;
157159
}
158160

159161
lscf->srv.ssl_sess_store_src_key = p;
160162

163+
p = ngx_copy(p, "ssl_session_store_by_lua",
164+
sizeof("ssl_session_store_by_lua") - 1);
161165
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
162166
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
163167
*p = '\0';

0 commit comments

Comments
 (0)