[libvirt] [PATCH] build: avoid compiler warning

Necessary on cygwin, where uid_t and gid_t are 4-byte long rather than int, causing gcc -Wformat warnings. * src/util/util.c (virFileOperationNoFork, virDirCreateNoFork) (virFileOperation, virDirCreate, virGetUserEnt): Cast uid_t and gid_t before passing to printf. * .gitignore: Ignore cygwin executables. --- .gitignore | 1 + src/util/util.c | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 8c275f4..4faf384 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *#*# *.#*# *.a +*.exe *.o *.orig *.rej diff --git a/src/util/util.c b/src/util/util.c index 3209185..0c5a22b 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -74,11 +74,14 @@ #include "util.h" #include "memory.h" #include "threads.h" +#include "verify.h" #ifndef NSIG # define NSIG 32 #endif +verify(sizeof (gid_t) <= sizeof (int) && sizeof (uid_t) <= sizeof (int)); + #define VIR_FROM_THIS VIR_FROM_NONE #define virUtilError(code, ...) \ @@ -1277,7 +1280,7 @@ static int virFileOperationNoFork(const char *path, int openflags, mode_t mode, && (fchown(fd, uid, gid) < 0)) { ret = errno; virReportSystemError(errno, _("cannot chown '%s' to (%u, %u)"), - path, uid, gid); + path, (int) uid, (int) gid); goto error; } if ((flags & VIR_FILE_OP_FORCE_PERMS) @@ -1328,7 +1331,7 @@ static int virDirCreateNoFork(const char *path, mode_t mode, uid_t uid, gid_t gi && (chown(path, uid, gid) < 0)) { ret = errno; virReportSystemError(errno, _("cannot chown '%s' to (%u, %u)"), - path, uid, gid); + path, (int) uid, (int) gid); goto error; } if ((flags & VIR_DIR_CREATE_FORCE_PERMS) @@ -1407,14 +1410,14 @@ parenterror: ret = errno; virReportSystemError(errno, _("cannot set gid %u creating '%s'"), - gid, path); + (int) gid, path); goto childerror; } if ((uid != 0) && (setuid(uid) != 0)) { ret = errno; virReportSystemError(errno, _("cannot set uid %u creating '%s'"), - uid, path); + (int) uid, path); goto childerror; } if ((fd = open(path, openflags, mode)) < 0) { @@ -1436,7 +1439,7 @@ parenterror: && (fchown(fd, -1, gid) < 0)) { ret = errno; virReportSystemError(errno, _("cannot chown '%s' to (%u, %u)"), - path, uid, gid); + path, (int) uid, (int) gid); goto childerror; } if ((flags & VIR_FILE_OP_FORCE_PERMS) @@ -1517,13 +1520,13 @@ parenterror: if ((gid != 0) && (setgid(gid) != 0)) { ret = errno; virReportSystemError(errno, _("cannot set gid %u creating '%s'"), - gid, path); + (int) gid, path); goto childerror; } if ((uid != 0) && (setuid(uid) != 0)) { ret = errno; virReportSystemError(errno, _("cannot set uid %u creating '%s'"), - uid, path); + (int) uid, path); goto childerror; } if (mkdir(path, mode) < 0) { @@ -1547,7 +1550,7 @@ parenterror: ret = errno; virReportSystemError(errno, _("cannot chown '%s' to group %u"), - path, gid); + path, (int) gid); goto childerror; } if ((flags & VIR_DIR_CREATE_FORCE_PERMS) @@ -2564,7 +2567,7 @@ static char *virGetUserEnt(uid_t uid, if (getpwuid_r(uid, &pwbuf, strbuf, strbuflen, &pw) != 0 || pw == NULL) { virReportSystemError(errno, _("Failed to find user record for uid '%d'"), - uid); + (int) uid); VIR_FREE(strbuf); return NULL; } -- 1.6.6.1

2010/5/3 Eric Blake <eblake@redhat.com>:
Necessary on cygwin, where uid_t and gid_t are 4-byte long rather than int, causing gcc -Wformat warnings.
* src/util/util.c (virFileOperationNoFork, virDirCreateNoFork) (virFileOperation, virDirCreate, virGetUserEnt): Cast uid_t and gid_t before passing to printf. * .gitignore: Ignore cygwin executables.
The .exe suffix is not cygwin specific. You should say here: Ignore Windows executables.
--- .gitignore | 1 + src/util/util.c | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-)
diff --git a/src/util/util.c b/src/util/util.c index 3209185..0c5a22b 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -74,11 +74,14 @@ #include "util.h" #include "memory.h" #include "threads.h" +#include "verify.h"
#ifndef NSIG # define NSIG 32 #endif
+verify(sizeof (gid_t) <= sizeof (int) && sizeof (uid_t) <= sizeof (int)); + #define VIR_FROM_THIS VIR_FROM_NONE
#define virUtilError(code, ...) \ @@ -1277,7 +1280,7 @@ static int virFileOperationNoFork(const char *path, int openflags, mode_t mode, && (fchown(fd, uid, gid) < 0)) { ret = errno; virReportSystemError(errno, _("cannot chown '%s' to (%u, %u)"), - path, uid, gid); + path, (int) uid, (int) gid);
Passing int for %u? Shouldn't this be a cast to unsigned int instead? Matthias

On 05/03/2010 03:41 PM, Matthias Bolte wrote:
@@ -1277,7 +1280,7 @@ static int virFileOperationNoFork(const char *path, int openflags, mode_t mode, && (fchown(fd, uid, gid) < 0)) { ret = errno; virReportSystemError(errno, _("cannot chown '%s' to (%u, %u)"), - path, uid, gid); + path, (int) uid, (int) gid);
Passing int for %u? Shouldn't this be a cast to unsigned int instead?
Technically, the two should match, to cater to weirdnix systems where signedness changes the underlying size of the data (at least, my understanding is that C99 allows such weirdness, even though no one produces hardware like that). But gcc doesn't warn about signed/unsigned mismatch, just about long/int mismatch, so the patch as is was sufficient to silence the warning. At any rate, you've convinced me; some uses were %u; others were %d (which, on cygwin, has the potential to show as a negative number, since it is entirely feasible to have a uid_t with the most significant bit set); I'm changing all uses to be %u and cast to unsigned. v2 patch coming up soon. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

Necessary on cygwin, where uid_t and gid_t are 4-byte long rather than int, causing gcc -Wformat warnings. * src/util/util.c (virFileOperationNoFork, virDirCreateNoFork) (virFileOperation, virDirCreate, virGetUserEnt): Cast uid_t and gid_t before passing to printf. * .gitignore: Ignore Windows executables. --- changes in v2: use unsigned int for all casts change a %d to %u tweak commit log wording .gitignore | 1 + src/util/util.c | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 8c275f4..4faf384 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *#*# *.#*# *.a +*.exe *.o *.orig *.rej diff --git a/src/util/util.c b/src/util/util.c index 3209185..e875d21 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -74,11 +74,15 @@ #include "util.h" #include "memory.h" #include "threads.h" +#include "verify.h" #ifndef NSIG # define NSIG 32 #endif +verify(sizeof (gid_t) <= sizeof (unsigned int) + && sizeof (uid_t) <= sizeof (unsigned int)); + #define VIR_FROM_THIS VIR_FROM_NONE #define virUtilError(code, ...) \ @@ -1277,7 +1281,7 @@ static int virFileOperationNoFork(const char *path, int openflags, mode_t mode, && (fchown(fd, uid, gid) < 0)) { ret = errno; virReportSystemError(errno, _("cannot chown '%s' to (%u, %u)"), - path, uid, gid); + path, (unsigned int) uid, (unsigned int) gid); goto error; } if ((flags & VIR_FILE_OP_FORCE_PERMS) @@ -1328,7 +1332,7 @@ static int virDirCreateNoFork(const char *path, mode_t mode, uid_t uid, gid_t gi && (chown(path, uid, gid) < 0)) { ret = errno; virReportSystemError(errno, _("cannot chown '%s' to (%u, %u)"), - path, uid, gid); + path, (unsigned int) uid, (unsigned int) gid); goto error; } if ((flags & VIR_DIR_CREATE_FORCE_PERMS) @@ -1407,14 +1411,14 @@ parenterror: ret = errno; virReportSystemError(errno, _("cannot set gid %u creating '%s'"), - gid, path); + (unsigned int) gid, path); goto childerror; } if ((uid != 0) && (setuid(uid) != 0)) { ret = errno; virReportSystemError(errno, _("cannot set uid %u creating '%s'"), - uid, path); + (unsigned int) uid, path); goto childerror; } if ((fd = open(path, openflags, mode)) < 0) { @@ -1436,7 +1440,7 @@ parenterror: && (fchown(fd, -1, gid) < 0)) { ret = errno; virReportSystemError(errno, _("cannot chown '%s' to (%u, %u)"), - path, uid, gid); + path, (unsigned int) uid, (unsigned int) gid); goto childerror; } if ((flags & VIR_FILE_OP_FORCE_PERMS) @@ -1517,13 +1521,13 @@ parenterror: if ((gid != 0) && (setgid(gid) != 0)) { ret = errno; virReportSystemError(errno, _("cannot set gid %u creating '%s'"), - gid, path); + (unsigned int) gid, path); goto childerror; } if ((uid != 0) && (setuid(uid) != 0)) { ret = errno; virReportSystemError(errno, _("cannot set uid %u creating '%s'"), - uid, path); + (unsigned int) uid, path); goto childerror; } if (mkdir(path, mode) < 0) { @@ -1547,7 +1551,7 @@ parenterror: ret = errno; virReportSystemError(errno, _("cannot chown '%s' to group %u"), - path, gid); + path, (unsigned int) gid); goto childerror; } if ((flags & VIR_DIR_CREATE_FORCE_PERMS) @@ -2563,8 +2567,8 @@ static char *virGetUserEnt(uid_t uid, */ if (getpwuid_r(uid, &pwbuf, strbuf, strbuflen, &pw) != 0 || pw == NULL) { virReportSystemError(errno, - _("Failed to find user record for uid '%d'"), - uid); + _("Failed to find user record for uid '%u'"), + (unsigned int) uid); VIR_FREE(strbuf); return NULL; } -- 1.6.6.1

2010/5/4 Eric Blake <eblake@redhat.com>:
Necessary on cygwin, where uid_t and gid_t are 4-byte long rather than int, causing gcc -Wformat warnings.
* src/util/util.c (virFileOperationNoFork, virDirCreateNoFork) (virFileOperation, virDirCreate, virGetUserEnt): Cast uid_t and gid_t before passing to printf. * .gitignore: Ignore Windows executables. ---
changes in v2: use unsigned int for all casts change a %d to %u tweak commit log wording
ACK. Matthias

On 05/03/2010 05:05 PM, Matthias Bolte wrote:
2010/5/4 Eric Blake <eblake@redhat.com>:
Necessary on cygwin, where uid_t and gid_t are 4-byte long rather than int, causing gcc -Wformat warnings.
* src/util/util.c (virFileOperationNoFork, virDirCreateNoFork) (virFileOperation, virDirCreate, virGetUserEnt): Cast uid_t and gid_t before passing to printf. * .gitignore: Ignore Windows executables. ---
changes in v2: use unsigned int for all casts change a %d to %u tweak commit log wording
ACK.
Thanks; pushed. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org
participants (2)
-
Eric Blake
-
Matthias Bolte