From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 09FB5205D6 for ; Tue, 10 Sep 2019 08:58:43 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id eTRGYIsRw0sA for ; Tue, 10 Sep 2019 08:58:42 -0400 (EDT) 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 turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id BDBDE200E7 for ; Tue, 10 Sep 2019 08:58:41 -0400 (EDT) From: Roman Khabibov Subject: [tarantool-patches] [PATCH] socket: better args handling for connect()/bind() Date: Tue, 10 Sep 2019 15:58:38 +0300 Message-Id: <20190910125838.52181-1-roman.habibov@tarantool.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-Help: List-Unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-Subscribe: List-Owner: List-post: List-Archive: To: tarantool-patches@freelists.org Cc: alexander.turenko@tarantool.org Check args types in socket.connect() and socket.bind() functions. 'host' should be a string and 'port' should be a string or a number. Part of #4138 --- Branch: https://github.com/tarantool/tarantool/tree/romanhabibov/gh-4138-getaddrinfo Issue: https://github.com/tarantool/tarantool/issues/4138 src/lua/socket.lua | 6 ++++-- test/app/socket.result | 25 +++++++++++++++++++++++++ test/app/socket.test.lua | 8 ++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/lua/socket.lua b/src/lua/socket.lua index e4815adbb..c4a61074e 100644 --- a/src/lua/socket.lua +++ b/src/lua/socket.lua @@ -1554,7 +1554,8 @@ local function lsocket_tcp() end local function lsocket_connect(host, port) - if host == nil or port == nil then + if type(host) ~= 'string' or (type(port) ~= 'string' and + type(port) ~= 'number') then error("Usage: luasocket.connect(host, port)") end local s, err = tcp_connect(host, port) @@ -1569,7 +1570,8 @@ local function lsocket_connect(host, port) end local function lsocket_bind(host, port, backlog) - if host == nil or port == nil then + if type(host) ~= 'string' or (type(port) ~= 'string' and + type(port) ~= '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 87b1d0387..20507bafb 100644 --- a/test/app/socket.result +++ b/test/app/socket.result @@ -2857,6 +2857,31 @@ check_err(err) --- - 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: '[string "socket.connect(127.0.0.1, 3301) "]:1: malformed number near ''127.0.0.1''' +... +socket.bind(127.0.0.1, 3301) +--- +- error: '[string "socket.bind(127.0.0.1, 3301) "]:1: malformed number near ''127.0.0.1''' +... +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])' +... test_run:cmd("clear filter") --- - true diff --git a/test/app/socket.test.lua b/test/app/socket.test.lua index 08cb7fc9e..1974e8cea 100644 --- a/test/app/socket.test.lua +++ b/test/app/socket.test.lua @@ -985,6 +985,14 @@ check_err(err) s, err = socket.connect('non_exists_hostname', 3301) check_err(err) +-- 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.connect({'127.0.0.1'}, 3301) +socket.bind({'127.0.0.1'}, 3301) + test_run:cmd("clear filter") -- case: sicket receive inconsistent behavior -- 2.20.1 (Apple Git-117)