From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 801146FC8F; Thu, 25 Mar 2021 00:27:39 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 801146FC8F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616621259; bh=fComPYFbgToqnxxN+BxMTSo1EDMum53hF1+PlXnVuoU=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=avFFZRZt/RcFQf9LsOBmmFQ7a8ncXEC4gxienh9nXYJr8nGdmicqM8CSgXGBcw1ac 3Ic+I2EbpQBNTtr4F+a8PiiPSz80DThBmc4g7s0hHtddWySb2tFMTgqaoudHVrnUzi TKi8IlDdZJsBAo+TuOqmbGB5hXTV6HuFVCPW44Do= Received: from smtp48.i.mail.ru (smtp48.i.mail.ru [94.100.177.108]) (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 6B7A66FFA3 for ; Thu, 25 Mar 2021 00:24:43 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 6B7A66FFA3 Received: by smtp48.i.mail.ru with esmtpa (envelope-from ) id 1lPAza-0004ib-Er; Thu, 25 Mar 2021 00:24:42 +0300 To: tarantool-patches@dev.tarantool.org, kyukhin@tarantool.org Date: Wed, 24 Mar 2021 22:24:26 +0100 Message-Id: <7247c1c4ee1a5d81f5e8e42acf880ddd884890ae.1616620860.git.v.shpilevoy@tarantool.org> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD9064ADF4728AA0EE922675D89A18E042B1EBC993A8274C79D182A05F5380850408E8F1948357060E850EA1233044D1185AF28B5A5D14A1B3F4D6DD4F486B875D0 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7AB5815F4DE05345AEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637993F4D0876F024C68638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C5DD32608FC869F5D4E4FEA2853DC1AE89AB68E0EF31731FCA471835C12D1D9774AD6D5ED66289B5278DA827A17800CE71AE4D56B06699BBC9FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C327ED053E960B195E117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CF7D36EA09806432C2BA3038C0950A5D36C8A9BA7A39EFB766EC990983EF5C0329BA3038C0950A5D36D5E8D9A59859A8B6A1D736784CFFC54B76E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B3BBE47FD9DD3FB59A8DF7F3B2552694A2BEBFE083D3B9BA73A03B725D353964BB11811A4A51E3B096D1867E19FE14079BA9C0B312567BB23089D37D7C0E48F6CA18204E546F3947CB26E97DCB74E625257739F23D657EF2BC8A9BA7A39EFB7666BA297DBC24807EA089D37D7C0E48F6C8AA50765F790063757B1FBEA53BC6EDBEFF80C71ABB335746BA297DBC24807EA27F269C8F02392CD6143B1329D1CCA5627F269C8F02392CD5571747095F342E88FB05168BE4CE3AF X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2368A440D3B0F6089093C9A16E5BC824AC8B6CDF511875BC4E8F7B195E1C97831148145A0506FFD488955918E15BC64BA X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C5DD32608FC869F5D4E4FEA2853DC1AE89AB68E0EF31731FC9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34181D1E89D5A0B42F80C4A3D007008039C0A36CE75F53B51500CD6E7D712A4DEFFE77BC72A7DEFE5F1D7E09C32AA3244C58B137227593F8F0D8B7ED11958AF76C3A76366E8A9DE7CA729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojjqzNotmU+gc4OU2obbfxpQ== X-Mailru-Sender: 504CC1E875BF3E7D9BC0E5172ADA3110A906815429F9D9606AAC7BC71CA7788D868BF0B7D181033107784C02288277CA03E0582D3806FB6A5317862B1921BA260ED6CFD6382C13A6112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH 14/15] sio: introduce and use sio_snprintf() X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Vladislav Shpilevoy via Tarantool-patches Reply-To: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" sio_strfaddr() can't be used in the places where static buffer is not acceptable - in any code which wants to push the value to Lua, or the address string must be long living. The patch introduces sio_snprintf(), which does the same, but saves the result into a provided buffer with a limited size. In the Lua C code the patch saves the address string on the stack which makes it safe against Lua GC interruptions. Part of #5632 (cherry picked from commit fde44b569bf920a08469b9569eab1701d4e57299) --- src/box/lua/session.c | 7 +++++-- src/sio.cc | 38 +++++++++++++++++++++++++------------- src/sio.h | 5 +++++ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/box/lua/session.c b/src/box/lua/session.c index d1d0da2d2..26315bc4b 100644 --- a/src/box/lua/session.c +++ b/src/box/lua/session.c @@ -264,10 +264,13 @@ lbox_session_peer(struct lua_State *L) struct sockaddr_storage addr; socklen_t addrlen = sizeof(addr); - if (sio_getpeername(fd, (struct sockaddr *)&addr, &addrlen) < 0) + struct sockaddr *addr_base = (struct sockaddr *)&addr; + if (sio_getpeername(fd, addr_base, &addrlen) < 0) luaL_error(L, "session.peer(): getpeername() failed"); - lua_pushstring(L, sio_strfaddr((struct sockaddr *)&addr, addrlen)); + char addrbuf[SERVICE_NAME_MAXLEN]; + sio_addr_snprintf(addrbuf, sizeof(addrbuf), addr_base, addrlen); + lua_pushstring(L, addrbuf); return 1; } diff --git a/src/sio.cc b/src/sio.cc index a425b8383..1f7800d6d 100644 --- a/src/sio.cc +++ b/src/sio.cc @@ -62,16 +62,17 @@ sio_socketname_to_buffer(int fd, char *buf, int size) return 0; struct sockaddr_storage addr; socklen_t addrlen = sizeof(addr); - int rc = getsockname(fd, (struct sockaddr *) &addr, &addrlen); + struct sockaddr *base_addr = (struct sockaddr *)&addr; + int rc = getsockname(fd, base_addr, &addrlen); if (rc == 0) { - SNPRINT(n, snprintf, buf, size, ", aka %s", - sio_strfaddr((struct sockaddr *)&addr, addrlen)); + SNPRINT(n, snprintf, buf, size, ", aka "); + SNPRINT(n, sio_addr_snprintf, buf, size, base_addr, addrlen); } addrlen = sizeof(addr); rc = getpeername(fd, (struct sockaddr *) &addr, &addrlen); if (rc == 0) { - SNPRINT(n, snprintf, buf, size, ", peer of %s", - sio_strfaddr((struct sockaddr *)&addr, addrlen)); + SNPRINT(n, snprintf, buf, size, ", peer of "); + SNPRINT(n, sio_addr_snprintf, buf, size, base_addr, addrlen); } return 0; } @@ -510,26 +511,37 @@ sio_getpeername(int fd, struct sockaddr *addr, socklen_t *addrlen) } /** Pretty print a peer address. */ -const char * -sio_strfaddr(struct sockaddr *addr, socklen_t addrlen) +int +sio_addr_snprintf(char *buf, size_t size, const struct sockaddr *addr, + socklen_t addrlen) { - static __thread char name[NI_MAXHOST + _POSIX_PATH_MAX + 2]; + int res; if (addr->sa_family == AF_UNIX) { struct sockaddr_un *u = (struct sockaddr_un *)addr; if (addrlen >= sizeof(*u)) - snprintf(name, sizeof(name), "unix/:%s", u->sun_path); + res = snprintf(buf, size, "unix/:%s", u->sun_path); else - snprintf(name, sizeof(name), "unix/:(socket)"); + res = snprintf(buf, size, "unix/:(socket)"); } else { char host[NI_MAXHOST], serv[NI_MAXSERV]; int flags = NI_NUMERICHOST | NI_NUMERICSERV; if (getnameinfo(addr, addrlen, host, sizeof(host), serv, sizeof(serv), flags) != 0) - snprintf(name, sizeof(name), "(host):(port)"); + res = snprintf(buf, size, "(host):(port)"); else if (addr->sa_family == AF_INET) - snprintf(name, sizeof(name), "%s:%s", host, serv); + res = snprintf(buf, size, "%s:%s", host, serv); else - snprintf(name, sizeof(name), "[%s]:%s", host, serv); + res = snprintf(buf, size, "[%s]:%s", host, serv); } + assert(res + 1 < SERVICE_NAME_MAXLEN); + assert(res >= 0); + return res; +} + +const char * +sio_strfaddr(struct sockaddr *addr, socklen_t addrlen) +{ + static __thread char name[SERVICE_NAME_MAXLEN]; + sio_addr_snprintf(name, sizeof(name), addr, addrlen); return name; } diff --git a/src/sio.h b/src/sio.h index 3b91f7eee..262bfcab8 100644 --- a/src/sio.h +++ b/src/sio.h @@ -57,6 +57,11 @@ enum { SERVICE_NAME_MAXLEN = 200, }; +/** Format the address into the given buffer. Behaves like snprintf(). */ +int +sio_addr_snprintf(char *buf, size_t size, const struct sockaddr *addr, + socklen_t addrlen); + const char * sio_strfaddr(struct sockaddr *addr, socklen_t addrlen); -- 2.24.3 (Apple Git-128)