[LTP] lib: New library function tst_get_free_uid

linuxtestproject.agent@gmail.com linuxtestproject.agent@gmail.com
Tue Jul 7 05:34:16 CEST 2026


Hi Wei,

On Tue, 07 Jul 2026 02:29:41 +0000, Wei Gao wrote:
> lib: New library function tst_get_free_uid

--- [PATCH 1/2] ---

> Add tst_get_free_uid() to dynamically find unused UIDs for tests.

The body restates what the subject already says.  What scenario or
test requires a dynamically allocated free UID that tst_get_uids()
cannot cover?

> +uid_t tst_get_free_uid_(const char *file, const int lineno, uid_t skip);
> +#define tst_get_free_uid(skip) tst_get_free_uid_(__FILE__, __LINE__, (skip))

tst_get_free_uid_() is a new public function in lib/ and include/.
LTP documentation rules require a kernel-doc /** ... */ comment for
new public symbols there.  Something like:

  /**
   * tst_get_free_uid() - Find a UID not assigned to any user.
   * @skip: UID value to skip (pass 0 to skip none).
   *
   * Scans the password database for the first unused UID starting
   * from 1, skipping @skip.  Calls tst_brk(TBROK) if no free UID
   * is found or a lookup error occurs.
   *
   * Return: An unused uid_t value.
   */

> - * Find unassigned gid. The skip argument can be used to ignore e.g. the main
> + * Find unassigned uid/gid. The skip argument can be used to ignore e.g. the main
>   * group of a specific user in case it's not listed in the group file. If you
> - * do not need to skip any specific gid, simply set it to 0.
> + * do not need to skip any specific id, simply set it to 0.

"not listed in the group file" applies to GIDs only.  UIDs live in
the password database (/etc/passwd or NSS passwd).  The comment
should say "password or group file", or be split per-function.

> +uid_t tst_get_free_uid_(const char *file, const int lineno, uid_t skip)
> +{
> +	uid_t ret;
> +
> +	errno = 0;
> +
> +	for (ret = 1; ret < MAX_UID; ret++) {
> +		if (ret == skip || getpwuid(ret))
> +			continue;
> +
> +		if (errno == 0 || errno == ENOENT || errno == ESRCH) {

POSIX requires callers to set errno = 0 before each call to
getpwuid() so that a NULL return with non-zero errno means error
rather than "entry not found".  The reset here is done once before
the loop.  If a successful getpwuid() call for an earlier UID leaves
errno non-zero (POSIX does not guarantee it is cleared on success),
the next NULL-returning call could be misclassified as an error and
trigger the tst_brk() path below.

A per-iteration reset:

  errno = 0;
  if (ret == skip || getpwuid(ret))
      continue;

would match the POSIX-recommended pattern and make the intent clear.

Verdict - Needs revision

Pre-existing issues:

tst_get_free_gid_() in lib/tst_uid.c has the same errno-reset issue
described above (errno set once before the loop, not before each
getgrgid() call).

---
Note:

The agent can sometimes produce false positives although often its
findings are genuine. If you find issues with the review, please
comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer


More information about the ltp mailing list