os: Xtranssock: fix possible string overflow warning in set_sun_path()

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 <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2026-11-13 15:43:14 +01:00
commit ae3c8b7ea1

View file

@ -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