From: Vladislav Shpilevoy via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: tarantool-patches@dev.tarantool.org, gorcunov@gmail.com,
sergepetrenko@tarantool.org
Subject: [Tarantool-patches] [PATCH 15/16] sio: introduce and use sio_snprintf()
Date: Sat, 20 Mar 2021 01:42:37 +0100 [thread overview]
Message-ID: <a5b3fae84352506bc10ca9580cdb2658a3b116b7.1616200860.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1616200860.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
---
src/box/iproto.cc | 8 +++++---
src/box/iproto.h | 2 +-
src/box/lua/info.c | 5 +++--
src/box/lua/session.c | 7 +++++--
src/lib/core/sio.c | 40 ++++++++++++++++++++++++++++------------
src/lib/core/sio.h | 4 ++++
6 files changed, 46 insertions(+), 20 deletions(-)
diff --git a/src/box/iproto.cc b/src/box/iproto.cc
index f7330af21..238842e17 100644
--- a/src/box/iproto.cc
+++ b/src/box/iproto.cc
@@ -138,12 +138,14 @@ static struct sockaddr_storage iproto_bound_address_storage;
static socklen_t iproto_bound_address_len;
const char *
-iproto_bound_address(void)
+iproto_bound_address(char *buf)
{
if (iproto_bound_address_len == 0)
return NULL;
- return sio_strfaddr((struct sockaddr *) &iproto_bound_address_storage,
- iproto_bound_address_len);
+ sio_addr_snprintf(buf, SERVICE_NAME_MAXLEN,
+ (struct sockaddr *) &iproto_bound_address_storage,
+ iproto_bound_address_len);
+ return buf;
}
/**
diff --git a/src/box/iproto.h b/src/box/iproto.h
index 392e4f08e..f6f7101a1 100644
--- a/src/box/iproto.h
+++ b/src/box/iproto.h
@@ -85,7 +85,7 @@ iproto_reset_stat(void);
* iproto. To be shown in box.info.
*/
const char *
-iproto_bound_address(void);
+iproto_bound_address(char *buf);
#if defined(__cplusplus)
} /* extern "C" */
diff --git a/src/box/lua/info.c b/src/box/lua/info.c
index c4c9fa0a0..1e0c0148a 100644
--- a/src/box/lua/info.c
+++ b/src/box/lua/info.c
@@ -52,7 +52,7 @@
#include "box/raft.h"
#include "lua/utils.h"
#include "fiber.h"
-#include "tt_static.h"
+#include "sio.h"
static void
lbox_pushvclock(struct lua_State *L, const struct vclock *vclock)
@@ -574,7 +574,8 @@ static int
lbox_info_listen(struct lua_State *L)
{
/* NULL is ok, no need to check. */
- lua_pushstring(L, iproto_bound_address());
+ char addrbuf[SERVICE_NAME_MAXLEN];
+ lua_pushstring(L, iproto_bound_address(addrbuf));
return 1;
}
diff --git a/src/box/lua/session.c b/src/box/lua/session.c
index 0a20aaad1..ae8c7094b 100644
--- a/src/box/lua/session.c
+++ b/src/box/lua/session.c
@@ -273,10 +273,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/lib/core/sio.c b/src/lib/core/sio.c
index 25c34ea59..6d1732332 100644
--- a/src/lib/core/sio.c
+++ b/src/lib/core/sio.c
@@ -59,16 +59,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;
}
@@ -326,26 +327,41 @@ sio_getsockname(int fd, struct sockaddr *addr, socklen_t *addrlen)
return 0;
}
-const char *
-sio_strfaddr(const struct sockaddr *addr, socklen_t addrlen)
+int
+sio_addr_snprintf(char *buf, size_t size, const struct sockaddr *addr,
+ socklen_t addrlen)
{
+ int res;
if (addr->sa_family == AF_UNIX) {
struct sockaddr_un *u = (struct sockaddr_un *)addr;
if (addrlen >= sizeof(*u))
- return tt_sprintf("unix/:%s", u->sun_path);
+ res = snprintf(buf, size, "unix/:%s", u->sun_path);
else
- return tt_sprintf("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)
- return tt_sprintf("(host):(port)");
+ res = snprintf(buf, size, "(host):(port)");
else if (addr->sa_family == AF_INET)
- return tt_sprintf("%s:%s", host, serv);
+ res = snprintf(buf, size, "%s:%s", host, serv);
else
- return tt_sprintf("[%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(const struct sockaddr *addr, socklen_t addrlen)
+{
+ int size = SERVICE_NAME_MAXLEN;
+ char *buf = (char *) static_reserve(size);
+ /* +1 for terminating 0. */
+ static_alloc(sio_addr_snprintf(buf, size, addr, addrlen) + 1);
+ return buf;
}
int
diff --git a/src/lib/core/sio.h b/src/lib/core/sio.h
index 4f5a7f6f3..016f1f079 100644
--- a/src/lib/core/sio.h
+++ b/src/lib/core/sio.h
@@ -73,6 +73,10 @@ sio_wouldblock(int err)
return err == EAGAIN || err == EWOULDBLOCK || err == EINTR;
}
+/** 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);
/**
* Format the address provided in struct sockaddr *addr.
--
2.24.3 (Apple Git-128)
next prev parent reply other threads:[~2021-03-20 0:46 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-20 0:42 [Tarantool-patches] [PATCH 00/16] Cord buffer, static alloc, and Lua GC bug Vladislav Shpilevoy via Tarantool-patches
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 01/16] fio: don't use shared buffer in pread() Vladislav Shpilevoy via Tarantool-patches
2021-03-22 7:19 ` Cyrill Gorcunov via Tarantool-patches
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 10/16] uri: replace static_alloc with ffi stash and ibuf Vladislav Shpilevoy via Tarantool-patches
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 11/16] buffer: remove static_alloc() from Lua Vladislav Shpilevoy via Tarantool-patches
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 12/16] lua: use lua_pushfstring() instead of tt_sprintf() Vladislav Shpilevoy via Tarantool-patches
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 13/16] sio: rework sio_strfaddr() Vladislav Shpilevoy via Tarantool-patches
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 14/16] sio: increase SERVICE_NAME_MAXLEN size Vladislav Shpilevoy via Tarantool-patches
2021-03-21 21:58 ` Cyrill Gorcunov via Tarantool-patches
2021-03-22 22:32 ` Vladislav Shpilevoy via Tarantool-patches
2021-03-23 6:56 ` Cyrill Gorcunov via Tarantool-patches
2021-03-20 0:42 ` Vladislav Shpilevoy via Tarantool-patches [this message]
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 16/16] buffer: remove Lua registers Vladislav Shpilevoy via Tarantool-patches
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 02/16] test: don't use IBUF_SHARED in the tests Vladislav Shpilevoy via Tarantool-patches
2021-03-22 7:35 ` Cyrill Gorcunov via Tarantool-patches
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 03/16] tuple: pass global ibuf explicitly where possible Vladislav Shpilevoy via Tarantool-patches
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 04/16] iconv: take errno before reseting the context Vladislav Shpilevoy via Tarantool-patches
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 05/16] cord_buf: introduce cord_buf API Vladislav Shpilevoy via Tarantool-patches
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 06/16] cord_buf: introduce ownership management Vladislav Shpilevoy via Tarantool-patches
2021-03-22 16:48 ` Serge Petrenko via Tarantool-patches
2021-03-22 22:32 ` Vladislav Shpilevoy via Tarantool-patches
2021-03-23 7:46 ` Serge Petrenko via Tarantool-patches
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 07/16] buffer: implement ffi stash Vladislav Shpilevoy via Tarantool-patches
2021-03-23 0:29 ` Vladislav Shpilevoy via Tarantool-patches
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 08/16] uuid: replace static_alloc with " Vladislav Shpilevoy via Tarantool-patches
2021-03-20 0:42 ` [Tarantool-patches] [PATCH 09/16] uuid: drop tt_uuid_str() from Lua Vladislav Shpilevoy via Tarantool-patches
2021-03-21 16:38 ` [Tarantool-patches] [PATCH 00/16] Cord buffer, static alloc, and Lua GC bug Vladislav Shpilevoy via Tarantool-patches
2021-03-22 7:52 ` Cyrill Gorcunov via Tarantool-patches
2021-03-22 7:56 ` Konstantin Osipov via Tarantool-patches
2021-03-22 17:17 ` Serge Petrenko via Tarantool-patches
2021-03-23 23:45 ` Vladislav Shpilevoy via Tarantool-patches
2021-03-24 13:28 ` 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=a5b3fae84352506bc10ca9580cdb2658a3b116b7.1616200860.git.v.shpilevoy@tarantool.org \
--to=tarantool-patches@dev.tarantool.org \
--cc=gorcunov@gmail.com \
--cc=sergepetrenko@tarantool.org \
--cc=v.shpilevoy@tarantool.org \
--subject='Re: [Tarantool-patches] [PATCH 15/16] 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