Skip to content

Commit 18b5de5

Browse files
spacewanderagentzh
authored andcommitted
bugfix: the upper bound of port ranges should be 65535 instead of 65536.
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent 9de3dc7 commit 18b5de5

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

src/ngx_http_lua_socket_tcp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
513513
if (n == 3) {
514514
port = luaL_checkinteger(L, 3);
515515

516-
if (port < 0 || port > 65536) {
516+
if (port < 0 || port > 65535) {
517517
lua_pushnil(L);
518518
lua_pushfstring(L, "bad port number: %d", port);
519519
return 2;
@@ -942,7 +942,7 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
942942
#else
943943
/* for nginx older than 1.5.8 */
944944

945-
len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
945+
len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1;
946946

947947
p = ngx_pnalloc(r->pool, len + sizeof(struct sockaddr_in));
948948
if (p == NULL) {

src/ngx_http_lua_socket_udp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ ngx_http_lua_socket_udp_setpeername(lua_State *L)
221221
if (n == 3) {
222222
port = luaL_checkinteger(L, 3);
223223

224-
if (port < 0 || port > 65536) {
224+
if (port < 0 || port > 65535) {
225225
lua_pushnil(L);
226226
lua_pushfstring(L, "bad port number: %d", port);
227227
return 2;
@@ -574,7 +574,7 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
574574
#else
575575
/* for nginx older than 1.5.8 */
576576

577-
len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
577+
len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1;
578578

579579
p = ngx_pnalloc(r->pool, len + sizeof(struct sockaddr_in));
580580
if (p == NULL) {

t/058-tcp-socket.t

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua;
44

55
repeat_each(2);
66

7-
plan tests => repeat_each() * 193;
7+
plan tests => repeat_each() * 196;
88

99
our $HtmlDir = html_dir;
1010

@@ -3727,3 +3727,22 @@ failed to connect: www.google.com could not be resolved
37273727
hello!
37283728
--- error_log eval
37293729
qr{\[alert\] .*? send\(\) failed \(\d+: Operation not permitted\) while resolving}
3730+
3731+
3732+
3733+
=== TEST 62: the upper bound of port range should be 2^16 - 1
3734+
--- config
3735+
location /t {
3736+
content_by_lua_block {
3737+
local sock, err = ngx.socket.connect("127.0.0.1", 65536)
3738+
if not sock then
3739+
ngx.say("failed to connect: ", err)
3740+
end
3741+
}
3742+
}
3743+
--- request
3744+
GET /t
3745+
--- response_body
3746+
failed to connect: bad port number: 65536
3747+
--- no_error_log
3748+
[error]

t/087-udp-socket.t

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,3 +1104,23 @@ qr/runtime error: content_by_lua\(nginx\.conf:\d+\):14: bad request/
11041104

11051105
--- no_error_log
11061106
[alert]
1107+
1108+
1109+
1110+
=== TEST 20: the upper bound of port range should be 2^16 - 1
1111+
--- config
1112+
location /t {
1113+
content_by_lua_block {
1114+
local sock = ngx.socket.udp()
1115+
local ok, err = sock:setpeername("127.0.0.1", 65536)
1116+
if not ok then
1117+
ngx.say("failed to connect: ", err)
1118+
end
1119+
}
1120+
}
1121+
--- request
1122+
GET /t
1123+
--- response_body
1124+
failed to connect: bad port number: 65536
1125+
--- no_error_log
1126+
[error]

0 commit comments

Comments
 (0)