From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 3A63A46970F for ; Thu, 21 Nov 2019 20:27:59 +0300 (MSK) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3594.4.19\)) From: Roman Khabibov In-Reply-To: <20191101123242.roqd2ayfoopur33o@tkn_work_nb> Date: Thu, 21 Nov 2019 20:27:58 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: <116AA5CD-0832-439C-B416-CEA65D96551C@tarantool.org> References: <20190910125838.52181-1-roman.habibov@tarantool.org> <20191101123242.roqd2ayfoopur33o@tkn_work_nb> Subject: Re: [Tarantool-patches] [PATCH] socket: better args handling for connect()/bind() List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org > On Nov 1, 2019, at 15:32, Alexander Turenko = wrote: >=20 > Please, rebase your commits on recent master and apply comments below. >=20 >> socket: better args handling for connect()/bind() >=20 > I would say about validation rather then handling. socket: better args validation for connect()/bind() >>=20 >> Check args types in socket.connect() and socket.bind() functions. >> 'host' should be a string and 'port' should be a string or a >> number. >>=20 >> Part of #4138 >=20 > It is better to place this commit before one that closes the issue. >=20 >> Branch: = https://github.com/tarantool/tarantool/tree/romanhabibov/gh-4138-getaddrin= fo >> Issue: https://github.com/tarantool/tarantool/issues/4138 >=20 >> @@ -1569,7 +1570,8 @@ local function lsocket_connect(host, port) >> end >>=20 >> local function lsocket_bind(host, port, backlog) >> - if host =3D=3D nil or port =3D=3D nil then >> + if type(host) ~=3D 'string' or (type(port) ~=3D 'string' and >> + type(port) ~=3D 'number') then >=20 > I would check backport against 'number' too (if it present). Backlog? @@ -1544,7 +1544,8 @@ local function lsocket_tcp() end =20 local function lsocket_connect(host, port) - if host =3D=3D nil or port =3D=3D nil then + if type(host) ~=3D 'string' or (type(port) ~=3D 'string' and + type(port) ~=3D 'number') then error("Usage: luasocket.connect(host, port)") end local s =3D tcp_connect(host, port) @@ -1556,7 +1557,8 @@ local function lsocket_connect(host, port) end =20 local function lsocket_bind(host, port, backlog) - if host =3D=3D nil or port =3D=3D nil then + if type(host) ~=3D 'string' or (type(port) ~=3D 'string' and = type(port) ~=3D + 'number') or (backlog ~=3D nil and type(backlog) ~=3D 'number') = then error("Usage: luasocket.bind(host, port [, backlog])") end local function prepare(s) return backlog end >> +socket.connect(127.0.0.1, 3301) >> +--- >> +- error: '[string "socket.connect(127.0.0.1, 3301) "]:1: malformed = number near ''127.0.0.1''' >=20 > Did you read this error? It is the parsing error from Lua interpreter. diff --git a/test/app/socket.result b/test/app/socket.result index fd299424c..6a72fb3cf 100644 --- a/test/app/socket.result +++ b/test/app/socket.result @@ -2812,6 +2812,31 @@ server:close() --- - true ... +-- gh-4138 Check the usage error in case of wrong args types. +socket.connect('127.0.0.1', {3301}) +--- +- error: 'builtin/socket.lua: Usage: luasocket.connect(host, port)' +... +socket.bind('127.0.0.1', {3301}) +--- +- error: 'builtin/socket.lua: Usage: luasocket.bind(host, port [, = backlog])' +... +socket.connect({'127.0.0.1'}, 3301) +--- +- error: 'builtin/socket.lua: Usage: luasocket.connect(host, port)' +... +socket.bind({'127.0.0.1'}, 3301) +--- +- error: 'builtin/socket.lua: Usage: luasocket.bind(host, port [, = backlog])' +... +socket.bind('127.0.0.1', 3301, {1}) +--- +- error: 'builtin/socket.lua: Usage: luasocket.bind(host, port [, = backlog])' +... +socket.bind('127.0.0.1', 3301, '1') +--- +- error: 'builtin/socket.lua: Usage: luasocket.bind(host, port [, = backlog])' +... test_run:cmd("clear filter") --- - true commit 5a0c462bb03a860034a0c23ef6d720b4aa2858dd Author: Roman Khabibov Date: Mon Sep 9 10:34:03 2019 +0300 socket: better args validation for connect()/bind() =20 Check args types in socket.connect() and socket.bind() functions. 'host' should be a string and 'port' should be a string or a number. =20 Part of #4138 diff --git a/src/lua/socket.lua b/src/lua/socket.lua index a334ad45b..2a4c69f45 100644 --- a/src/lua/socket.lua +++ b/src/lua/socket.lua @@ -1544,7 +1544,8 @@ local function lsocket_tcp() end =20 local function lsocket_connect(host, port) - if host =3D=3D nil or port =3D=3D nil then + if type(host) ~=3D 'string' or (type(port) ~=3D 'string' and + type(port) ~=3D 'number') then error("Usage: luasocket.connect(host, port)") end local s =3D tcp_connect(host, port) @@ -1556,7 +1557,8 @@ local function lsocket_connect(host, port) end =20 local function lsocket_bind(host, port, backlog) - if host =3D=3D nil or port =3D=3D nil then + if type(host) ~=3D 'string' or (type(port) ~=3D 'string' and = type(port) ~=3D + 'number') or (backlog ~=3D nil and type(backlog) ~=3D 'number') = then error("Usage: luasocket.bind(host, port [, backlog])") end local function prepare(s) return backlog end diff --git a/test/app/socket.result b/test/app/socket.result index fd299424c..6a72fb3cf 100644 --- a/test/app/socket.result +++ b/test/app/socket.result @@ -2812,6 +2812,31 @@ server:close() --- - true ... +-- gh-4138 Check the usage error in case of wrong args types. +socket.connect('127.0.0.1', {3301}) +--- +- error: 'builtin/socket.lua: Usage: luasocket.connect(host, port)' +... +socket.bind('127.0.0.1', {3301}) +--- +- error: 'builtin/socket.lua: Usage: luasocket.bind(host, port [, = backlog])' +... +socket.connect({'127.0.0.1'}, 3301) +--- +- error: 'builtin/socket.lua: Usage: luasocket.connect(host, port)' +... +socket.bind({'127.0.0.1'}, 3301) +--- +- error: 'builtin/socket.lua: Usage: luasocket.bind(host, port [, = backlog])' +... +socket.bind('127.0.0.1', 3301, {1}) +--- +- error: 'builtin/socket.lua: Usage: luasocket.bind(host, port [, = backlog])' +... +socket.bind('127.0.0.1', 3301, '1') +--- +- error: 'builtin/socket.lua: Usage: luasocket.bind(host, port [, = backlog])' +... test_run:cmd("clear filter") --- - true diff --git a/test/app/socket.test.lua b/test/app/socket.test.lua index c72d41763..022cd4f40 100644 --- a/test/app/socket.test.lua +++ b/test/app/socket.test.lua @@ -958,6 +958,14 @@ fiber.cancel(echo_fiber) client:read(1, 5) =3D=3D '' server:close() =20 +-- gh-4138 Check the usage error in case of wrong args types. +socket.connect('127.0.0.1', {3301}) +socket.bind('127.0.0.1', {3301}) +socket.connect({'127.0.0.1'}, 3301) +socket.bind({'127.0.0.1'}, 3301) +socket.bind('127.0.0.1', 3301, {1}) +socket.bind('127.0.0.1', 3301, '1') + test_run:cmd("clear filter") =20 -- case: sicket receive inconsistent behavior