[Tarantool-patches] [PATCH 15/16] sio: introduce and use sio_snprintf()
Vladislav Shpilevoy
v.shpilevoy at tarantool.org
Sat Mar 20 03:42:37 MSK 2021
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)
More information about the Tarantool-patches
mailing list