]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/subordinateio.c: find_free_range(): Use id_t instead of u_long
authorAlejandro Colomar <alx@kernel.org>
Sun, 15 Mar 2026 14:31:09 +0000 (15:31 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 10 Apr 2026 03:20:34 +0000 (22:20 -0500)
It's the natural type for this API, and it's also more
consistent with its wrappers.

Let's also use literal -1 for the error code, which is safer than
unsigned constants, as -1 is sign-extended to fit whatever unsigned type
we're using.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/find_new_sub_gids.c
lib/find_new_sub_uids.c
lib/subordinateio.c

index e4ee7e908a0cc88f5392982ce04299f9e5df91ba..591fe2df2466bcb9b1b1c44ded07c440dcde66a9 100644 (file)
@@ -44,7 +44,7 @@ int find_new_sub_gids (id_t *range_start, unsigned long *range_count)
        }
 
        start = sub_gid_find_free_range(min, max, count);
-       if (start == (id_t)-1) {
+       if (start == -1) {
                errno = EUSERS;
                return -1;
        }
index 50c3c830bc8e782273233d78f4511bee78196ed8..8521e802c6b3177c6b1eb2549947907a15b970df 100644 (file)
@@ -44,7 +44,7 @@ int find_new_sub_uids (id_t *range_start, unsigned long *range_count)
        }
 
        start = sub_uid_find_free_range(min, max, count);
-       if (start == (id_t)-1) {
+       if (start == -1) {
                errno = EUSERS;
                return -1;
        }
index 19c677dd89109529c9d26e69e71177823adeccb5..c8dcb2ad50ecd910a4662572982eb5ca2116fa2d 100644 (file)
@@ -327,14 +327,13 @@ static int subordinate_range_cmp (const void *p1, const void *p2)
  * @max: the highest uid to find
  * @count: the number of uids needed
  *
- * Return the lowest new uid, or ULONG_MAX on failure.
+ * Return the lowest new uid, or -1 on failure.
  */
-static unsigned long find_free_range(struct commonio_db *db,
-                                    unsigned long min, unsigned long max,
-                                    unsigned long count)
+static id_t
+find_free_range(struct commonio_db *db, id_t min, id_t max, unsigned long count)
 {
+       id_t                           low, high;
        const struct subordinate_range *range;
-       unsigned long low, high;
 
        /* When given invalid parameters fail */
        if ((count == 0) || (max < min))
@@ -346,8 +345,10 @@ static unsigned long find_free_range(struct commonio_db *db,
 
        low = min;
        while (NULL != (range = commonio_next(db))) {
-               unsigned long first = range->start;
-               unsigned long last = first + range->count - 1;
+               id_t  first, last;
+
+               first = range->start;
+               last = first + range->count - 1;
 
                /* Find the top end of the hole before this range */
                high = first;
@@ -372,7 +373,7 @@ static unsigned long find_free_range(struct commonio_db *db,
        if (((max - low) + 1) >= count)
                return low;
 fail:
-       return ULONG_MAX;
+       return -1;
 }
 
 /*
@@ -673,9 +674,7 @@ int sub_uid_unlock (bool process_selinux)
 
 uid_t sub_uid_find_free_range(uid_t min, uid_t max, unsigned long count)
 {
-       unsigned long start;
-       start = find_free_range (&subordinate_uid_db, min, max, count);
-       return start == ULONG_MAX ? (uid_t) -1 : start;
+       return find_free_range(&subordinate_uid_db, min, max, count);
 }
 
 
@@ -825,9 +824,7 @@ int sub_gid_unlock (bool process_selinux)
 
 gid_t sub_gid_find_free_range(gid_t min, gid_t max, unsigned long count)
 {
-       unsigned long start;
-       start = find_free_range (&subordinate_gid_db, min, max, count);
-       return start == ULONG_MAX ? (gid_t) -1 : start;
+       return find_free_range(&subordinate_gid_db, min, max, count);
 }
 
 /*
@@ -995,6 +992,7 @@ int find_subid_owners(unsigned long id, enum subid_type id_type, uid_t **uids)
 
 bool new_subid_range(struct subordinate_range *range, enum subid_type id_type, bool reuse)
 {
+       id_t               start;
        struct commonio_db *db;
        const struct subordinate_range *r;
        bool ret;
@@ -1044,12 +1042,12 @@ bool new_subid_range(struct subordinate_range *range, enum subid_type id_type, b
                }
        }
 
-       range->start = find_free_range(db, range->start, ULONG_MAX, range->count);
-
-       if (range->start == ULONG_MAX) {
+       start = find_free_range(db, range->start, -1, range->count);
+       if (start == -1) {
                ret = false;
                goto out;
        }
+       range->start = start;
 
        ret = add_range(db, range->owner, range->start, range->count) == 1;