Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: tarantool-patches@dev.tarantool.org, kyukhin@tarantool.org
Subject: [Tarantool-patches] [PATCH 14/15] sio: introduce and use sio_snprintf()
Date: Wed, 24 Mar 2021 22:24:26 +0100	[thread overview]
Message-ID: <7247c1c4ee1a5d81f5e8e42acf880ddd884890ae.1616620860.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1616620860.git.v.shpilevoy@tarantool.org>

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)


  parent reply	other threads:[~2021-03-24 21:27 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-24 21:24 [Tarantool-patches] [PATCH 00/15] Cord buffer, static alloc, and Lua GC bug for 1.10 Vladislav Shpilevoy via Tarantool-patches
2021-03-24 21:24 ` [Tarantool-patches] [PATCH 01/15] fio: don't use shared buffer in pread() Vladislav Shpilevoy via Tarantool-patches
2021-03-24 21:24 ` [Tarantool-patches] [PATCH 10/15] uri: replace static_alloc with ffi stash and ibuf Vladislav Shpilevoy via Tarantool-patches
2021-03-24 21:24 ` [Tarantool-patches] [PATCH 11/15] lua: use lua_pushfstring() instead of tt_sprintf() Vladislav Shpilevoy via Tarantool-patches
2021-03-24 21:24 ` [Tarantool-patches] [PATCH 12/15] sio: rework sio_strfaddr() Vladislav Shpilevoy via Tarantool-patches
2021-03-24 21:24 ` [Tarantool-patches] [PATCH 13/15] sio: increase SERVICE_NAME_MAXLEN size Vladislav Shpilevoy via Tarantool-patches
2021-03-24 21:24 ` Vladislav Shpilevoy via Tarantool-patches [this message]
2021-03-24 21:24 ` [Tarantool-patches] [PATCH 15/15] buffer: remove Lua registers Vladislav Shpilevoy via Tarantool-patches
2021-03-24 21:24 ` [Tarantool-patches] [PATCH 02/15] test: don't use IBUF_SHARED in the tests Vladislav Shpilevoy via Tarantool-patches
2021-03-24 21:24 ` [Tarantool-patches] [PATCH 03/15] tuple: pass global ibuf explicitly where possible Vladislav Shpilevoy via Tarantool-patches
2021-03-24 21:24 ` [Tarantool-patches] [PATCH 04/15] iconv: take errno before reseting the context Vladislav Shpilevoy via Tarantool-patches
2021-03-24 21:24 ` [Tarantool-patches] [PATCH 05/15] cord_buf: introduce cord_buf API Vladislav Shpilevoy via Tarantool-patches
2021-03-24 21:24 ` [Tarantool-patches] [PATCH 06/15] cord_buf: introduce ownership management Vladislav Shpilevoy via Tarantool-patches
2021-03-24 21:24 ` [Tarantool-patches] [PATCH 07/15] buffer: implement ffi stash Vladislav Shpilevoy via Tarantool-patches
2021-03-24 21:24 ` [Tarantool-patches] [PATCH 08/15] uuid: replace static_alloc with " Vladislav Shpilevoy via Tarantool-patches
2021-03-24 21:24 ` [Tarantool-patches] [PATCH 09/15] uuid: drop tt_uuid_str() from Lua Vladislav Shpilevoy via Tarantool-patches
2021-03-29 15:41 ` [Tarantool-patches] [PATCH 00/15] Cord buffer, static alloc, and Lua GC bug for 1.10 Kirill Yukhin via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7247c1c4ee1a5d81f5e8e42acf880ddd884890ae.1616620860.git.v.shpilevoy@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=kyukhin@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 14/15] sio: introduce and use sio_snprintf()' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox