From ae3c8b7ea1308835f2eb9c4554f69497598d413f Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Fri, 13 Nov 2026 15:43:14 +0100 Subject: [PATCH] os: Xtranssock: fix possible string overflow warning in set_sun_path() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In file included from ../os/transport.c:65: ../os/Xtranssock.c: In function ‘_XSERVTransSocketUNIXCreateListener’: ../os/Xtranssock.c:631:45: error: ‘%s’ directive output may be truncated writing up to 107 bytes into a region of size between 91 and 108 [-Werror=format-truncation=] 631 | snprintf(path, sizeof(s.sun_path), "%s%s%s", at, upath, port); | ^~ In file included from /usr/include/stdio.h:974, from /usr/include/x86_64-linux-gnu/bsd/stdio.h:32, from /usr/include/x86_64-linux-gnu/bsd/libutil.h:49, from /usr/include/x86_64-linux-gnu/bsd/stdlib.h:37, from ../os/transport.c:51: Signed-off-by: Enrico Weigelt, metux IT consult --- os/Xtranssock.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/os/Xtranssock.c b/os/Xtranssock.c index c1091b21ac..54476143a0 100644 --- a/os/Xtranssock.c +++ b/os/Xtranssock.c @@ -610,7 +610,7 @@ static int set_sun_path(const char *port, const char *upath, char *path, int abstract) { struct sockaddr_un s; - ssize_t maxlen = sizeof(s.sun_path) - 1; + size_t atlen, upathlen, portlen, len; const char *at = ""; if (!port || !*port || !path) @@ -626,9 +626,16 @@ set_sun_path(const char *port, const char *upath, char *path, int abstract) if (*port == '/') /* a full pathname */ upath = ""; - if ((ssize_t)(strlen(at) + strlen(upath) + strlen(port)) > maxlen) + atlen = strlen(at); + upathlen = strlen(upath); + portlen = strlen(port); + len = atlen + upathlen + portlen; + if (len >= sizeof(s.sun_path)) return -1; - snprintf(path, sizeof(s.sun_path), "%s%s%s", at, upath, port); + + memcpy(path, at, atlen); + memcpy(path + atlen, upath, upathlen); + memcpy(path + atlen + upathlen, port, portlen + 1); return 0; } #endif