mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
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:
11
lib/launch.c
11
lib/launch.c
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user