Skip to content

Commit ed8c7fb

Browse files
ytcoodebrauner
authored andcommitted
fs/file: fix the check in find_next_fd()
The maximum possible return value of find_next_zero_bit(fdt->full_fds_bits, maxbit, bitbit) is maxbit. This return value, multiplied by BITS_PER_LONG, gives the value of bitbit, which can never be greater than maxfd, it can only be equal to maxfd at most, so the following check 'if (bitbit > maxfd)' will never be true. Moreover, when bitbit equals maxfd, it indicates that there are no unused fds, and the function can directly return. Fix this check. Signed-off-by: Yuntao Wang <yuntao.wang@linux.dev> Link: https://lore.kernel.org/r/20240529160656.209352-1-yuntao.wang@linux.dev Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent a82c13d commit ed8c7fb

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

fs/file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,12 @@ struct files_struct init_files = {
486486

487487
static unsigned int find_next_fd(struct fdtable *fdt, unsigned int start)
488488
{
489-
unsigned int maxfd = fdt->max_fds;
489+
unsigned int maxfd = fdt->max_fds; /* always multiple of BITS_PER_LONG */
490490
unsigned int maxbit = maxfd / BITS_PER_LONG;
491491
unsigned int bitbit = start / BITS_PER_LONG;
492492

493493
bitbit = find_next_zero_bit(fdt->full_fds_bits, maxbit, bitbit) * BITS_PER_LONG;
494-
if (bitbit > maxfd)
494+
if (bitbit >= maxfd)
495495
return maxfd;
496496
if (bitbit > start)
497497
start = bitbit;

0 commit comments

Comments
 (0)