HomeFreeBSD

free_blocks(): Fix reports from 2016 PVS Studio FreeBSD report

Description

free_blocks(): Fix reports from 2016 PVS Studio FreeBSD report

In 2016, the authors of PVS Studio ran it on the FreeBSD kernel, which
identified a number of bugs / cleanup opportunities in the FreeBSD ZFS kernel
code. A few of them persist to the present day:

https://reviews.freebsd.org/D5245

Note that the scan was done against
freebsd/freebsd-src@46763fd4ca8a37f836c9bf2333f9d687509278f3.

In particular, we have the following in free_blocks():

\sys\cddl\contrib\opensolaris\uts\common\fs\zfs\dnode_sync.c (174): error V547: Expression 'left >= right' is always true. Unsigned type value is always >= 0.
\sys\cddl\contrib\opensolaris\uts\common\fs\zfs\dnode_sync.c (171): error V634: The priority of the '*' operation is higher than that of the '<<' operation. It's possible that parentheses should be used in the expression.
\sys\cddl\contrib\opensolaris\uts\common\fs\zfs\dnode_sync.c (175): error V547: Expression 'left >= right' is always true. Unsigned type value is always >= 0.

A couple of assertions accidentally typecast the arguments they check to
unsigned in such a way that the result is always true. Also, parentheses
are missing around 1<<epbs in (db->db_blkid * 1<<epbs). This works
out to be okay due to multiplication not caring what order of operations
we use, but it is better to fix it to be (db->db_blkid << epbs).

A few of the function local variables probably never should have been
32-bit in the first place, so we make them 64-bit. We also replace the
existing assertions with additional assertions to ensure that 64-bit
unsigned arithmetic is safe.

Reviewed-by: Alexander Motin <mav@FreeBSD.org>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #14407

Details

Provenance
Richard Yao <richard.yao@alumni.stonybrook.edu>Authored on Jan 23 2023, 9:12 PM
GitHub <noreply@github.com>Committed on Jan 23 2023, 9:12 PM
Parents
rG71974946be7e: Fix reading uninitialized variable in receive_read
Branches
Unknown
Tags
Unknown

Event Timeline

GitHub <noreply@github.com> committed rGf091db924883: free_blocks(): Fix reports from 2016 PVS Studio FreeBSD report (authored by Richard Yao <richard.yao@alumni.stonybrook.edu>).Jan 23 2023, 9:12 PM