lib: Better handling for problems creating the socket path

GCC 12 gives a warning about our previous attempt to check the length
of the socket path.  In the ensuing discussion it was pointed out that
it is easier to get snprintf to do the hard work.  snprintf will
return an int >= UNIX_PATH_MAX if the path is too long, or < 0 if
there are other errors such as locale/encoding problems.  So we should
just check for those two cases instead.

https://lists.fedoraproject.org/archives/list/devel@lists.fedoraproject.org/thread/NPKWMTSJ2A2ABNJJEH6WTZIAEFTX6CQY/

Thanks: Martin Sebor and Laszlo Ersek
This commit is contained in:
Richard W.M. Jones
2022-01-17 12:35:30 +00:00
parent d1e7e1a323
commit f019cc01b0

View File

@@ -325,15 +325,20 @@ int
guestfs_int_create_socketname (guestfs_h *g, const char *filename,
char (*sockpath)[UNIX_PATH_MAX])
{
int r;
if (guestfs_int_lazy_make_sockdir (g) == -1)
return -1;
if (strlen (g->sockdir) + 1 + strlen (filename) > UNIX_PATH_MAX-1) {
r = snprintf (*sockpath, UNIX_PATH_MAX, "%s/%s", g->sockdir, filename);
if (r >= UNIX_PATH_MAX) {
error (g, _("socket path too long: %s/%s"), g->sockdir, filename);
return -1;
}
snprintf (*sockpath, UNIX_PATH_MAX, "%s/%s", g->sockdir, filename);
if (r < 0) {
perrorf (g, _("%s"), g->sockdir);
return -1;
}
return 0;
}