Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/ngx_http_lua_balancer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1006,8 +1006,12 @@ ngx_http_lua_ffi_balancer_bind_to_local_addr(ngx_http_request_t *r,
return NGX_ERROR;
}

/*
* NGX_DECLINED means addr is not an address, and local->sockaddr is
* left unset then, so it must not be published to bp->local either.
*/
rc = ngx_parse_addr_port(r->pool, local, (u_char *) addr, addr_len);
if (rc == NGX_ERROR) {
if (rc != NGX_OK) {
p = ngx_snprintf(errbuf, *errbuf_size, "invalid addr %s", addr);
*errbuf_size = p - errbuf;
return NGX_ERROR;
Expand Down
35 changes: 35 additions & 0 deletions t/138-balancer-upstream-bind.t
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,38 @@ GET /t
127.0.0.2
--- no_error_log
[error]



=== TEST 6: bind to an invalid address in balancer phase
--- no_http2
--- http_config
lua_package_path "$TEST_NGINX_SERVER_ROOT/html/?.lua;;";

upstream backend {
server 0.0.0.1;
balancer_by_lua_block {
local balancer = require "ngx.balancer"
assert(balancer.bind_to_local_addr("127.0.0.2"))
local ok, err = balancer.bind_to_local_addr("not-an-ip")
if not ok then
ngx.log(ngx.ERR, "failed to bind: ", err)
end
assert(balancer.set_current_peer("127.0.0.1", ngx.var.server_port))
}
}
--- config
location = /t {
proxy_pass http://backend/back;
}

location = /back {
echo $remote_addr;
}

--- request
GET /t
--- response_body
127.0.0.2
--- error_log
failed to bind: invalid addr not-an-ip
Loading