Fix unsigned comparison warnings in chown()
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 30 Dec 2025 08:58:00 +0000 (09:58 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 31 Dec 2025 11:11:25 +0000 (12:11 +0100)
Original warnings:
src/filefunc.cpp:756:12: error: comparison of unsigned expression in '< 0' is always false [-Werror=type-limits]
src/filefunc.cpp:758:12: error: comparison of unsigned expression in '< 0' is always false [-Werror=type-limits]
src/filefunc.cpp:759:12: error: comparison of unsigned expression in '< 0' is always false [-Werror=type-limits]

The uid_t and gid_t types are unsigned, so comparisons like 'uid < 0' are always false.
Changed to use static_cast<uid_t>(-1) and static_cast<gid_t>(-1) to check for sentinel values.

src/filefunc.cpp

index fbb8903..49358ff 100644 (file)
@@ -752,13 +752,17 @@ bool chmod(const std::string& path, int mode)
  */
 bool chown(const std::string& path, const I2n::User& user, const I2n::Group& group)
 {
-    uid_t uid= user.Uid;
-    if (uid<0) return false;
-    gid_t gid= group.Gid;
-    if (gid<0) gid= user.Gid;
-    if (gid<0) return false;
-    int res= ::chown( path.c_str(), uid, gid);
-    return (res==0);
+    uid_t uid = user.Uid;
+    gid_t gid = group.Gid;
+    // Use sentinel value check for unsigned types
+    if (uid == static_cast<uid_t>(-1))
+        return false;
+    if (gid == static_cast<gid_t>(-1))
+        gid = user.Gid;
+    if (gid == static_cast<gid_t>(-1))
+        return false;
+    int res = ::chown(path.c_str(), uid, gid);
+    return (res == 0);
 } // eo chown(const std::string&,const User&,const Group&)
 
 /**