From abc11d7b6ae0c79f940a5cd3dd3393dcb338277e Mon Sep 17 00:00:00 2001 From: nop Date: Tue, 12 May 2026 12:50:01 +0800 Subject: [PATCH] Allow socket.listen on port 0 (#2152) * Allow socket.listen on port 0 * Validate port string parsed from address --------- Co-authored-by: byte.nop --- lualib-src/lua-socket.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lualib-src/lua-socket.c b/lualib-src/lua-socket.c index bfb0ad04..aad50623 100644 --- a/lualib-src/lua-socket.c +++ b/lualib-src/lua-socket.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -411,6 +412,20 @@ lunpack(lua_State *L) { return 4; } +static int +parse_port(lua_State *L, const char *s, const char *addr) { + errno = 0; + + char *endptr; + unsigned long port = strtoul(s, &endptr, 10); + + if (errno != 0 || endptr == s || *endptr != '\0' || port > UINT16_MAX) { + luaL_error(L, "Invalid port in address %s.", addr); + } + + return (int)port; +} + static const char * address_port(lua_State *L, char *tmp, const char * addr, int port_index, int *port) { const char * host; @@ -430,7 +445,7 @@ address_port(lua_State *L, char *tmp, const char * addr, int port_index, int *po if (sep == NULL) { luaL_error(L, "Invalid address %s.",addr); } - *port = strtoul(sep+1,NULL,10); + *port = parse_port(L, sep + 1, addr); } else { // is ipv4 const char * sep = strchr(addr,':'); @@ -440,7 +455,7 @@ address_port(lua_State *L, char *tmp, const char * addr, int port_index, int *po memcpy(tmp, addr, sep-addr); tmp[sep-addr] = '\0'; host = tmp; - *port = strtoul(sep+1,NULL,10); + *port = parse_port(L, sep + 1, addr); } } else { host = addr; @@ -489,9 +504,6 @@ llisten(lua_State *L) { char tmp[sz]; int port = 0; const char * host = address_port(L, tmp, addr, 2, &port); - if (port == 0) { - return luaL_error(L, "Invalid port"); - } int backlog = luaL_optinteger(L,3,BACKLOG); struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); int id = skynet_socket_listen(ctx, host,port,backlog);