From: Alejandro Colomar Date: Sun, 15 Mar 2026 14:31:09 +0000 (+0100) Subject: lib/subordinateio.c: find_free_range(): Use id_t instead of u_long X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=de7f1c78f70f8df4b2956f7363da3774348bc58e;p=thirdparty%2Fshadow.git lib/subordinateio.c: find_free_range(): Use id_t instead of u_long 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 --- diff --git a/lib/find_new_sub_gids.c b/lib/find_new_sub_gids.c index e4ee7e908..591fe2df2 100644 --- a/lib/find_new_sub_gids.c +++ b/lib/find_new_sub_gids.c @@ -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; } diff --git a/lib/find_new_sub_uids.c b/lib/find_new_sub_uids.c index 50c3c830b..8521e802c 100644 --- a/lib/find_new_sub_uids.c +++ b/lib/find_new_sub_uids.c @@ -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; } diff --git a/lib/subordinateio.c b/lib/subordinateio.c index 19c677dd8..c8dcb2ad5 100644 --- a/lib/subordinateio.c +++ b/lib/subordinateio.c @@ -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;