Skip to content

Commit 7d189b3

Browse files
perfgaoclaude
authored andcommitted
bugfix: balancer.bind_to_local_addr() crashed the worker on an invalid address.
ngx_parse_addr_port() returns NGX_DECLINED when the text is not an address; NGX_ERROR only means an allocation failed. The FFI function only checked for NGX_ERROR, so an invalid address such as "not-an-ip" was reported as success, and an ngx_addr_t whose sockaddr was never set (it comes from ngx_palloc()) was published to bp->local. The upstream connect then called ngx_inet_get_port() and bind() on that pointer, and the worker exited on signal 11. Treat anything but NGX_OK as an invalid address and keep the previous bp->local, as ngx_stream_lua_ffi_balancer_bind_to_local_addr() already does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d9d95ab commit 7d189b3

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎src/ngx_http_lua_balancer.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,8 +1006,12 @@ ngx_http_lua_ffi_balancer_bind_to_local_addr(ngx_http_request_t *r,
10061006
return NGX_ERROR;
10071007
}
10081008

1009+
/*
1010+
* NGX_DECLINED means addr is not an address, and local->sockaddr is
1011+
* left unset then, so it must not be published to bp->local either.
1012+
*/
10091013
rc = ngx_parse_addr_port(r->pool, local, (u_char *) addr, addr_len);
1010-
if (rc == NGX_ERROR) {
1014+
if (rc != NGX_OK) {
10111015
p = ngx_snprintf(errbuf, *errbuf_size, "invalid addr %s", addr);
10121016
*errbuf_size = p - errbuf;
10131017
return NGX_ERROR;

‎t/138-balancer-upstream-bind.t‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,38 @@ GET /t
173173
127.0.0.2
174174
--- no_error_log
175175
[error]
176+
177+
178+
179+
=== TEST 6: bind to an invalid address in balancer phase
180+
--- no_http2
181+
--- http_config
182+
lua_package_path "$TEST_NGINX_SERVER_ROOT/html/?.lua;;";
183+
184+
upstream backend {
185+
server 0.0.0.1;
186+
balancer_by_lua_block {
187+
local balancer = require "ngx.balancer"
188+
assert(balancer.bind_to_local_addr("127.0.0.2"))
189+
local ok, err = balancer.bind_to_local_addr("not-an-ip")
190+
if not ok then
191+
ngx.log(ngx.ERR, "failed to bind: ", err)
192+
end
193+
assert(balancer.set_current_peer("127.0.0.1", ngx.var.server_port))
194+
}
195+
}
196+
--- config
197+
location = /t {
198+
proxy_pass http://backend/back;
199+
}
200+
201+
location = /back {
202+
echo $remote_addr;
203+
}
204+
205+
--- request
206+
GET /t
207+
--- response_body
208+
127.0.0.2
209+
--- error_log
210+
failed to bind: invalid addr not-an-ip

0 commit comments

Comments
 (0)