Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : /**
4 : : * @brief Convert POSIX `st_blocks` units into bytes.
5 : : *
6 : : * @details POSIX defines `st_blocks` in 512-byte units regardless of the
7 : : * filesystem I/O block size. Some filesystems may report non-positive values
8 : : * for special files; those are normalized to zero.
9 : : *
10 : : * @param blocks Allocated block count from file metadata.
11 : : * @return Allocated bytes as `blocks * POSIX_STAT_BLOCK_BYTES`, or zero when
12 : : * @p blocks is less than or equal to zero, or SIZE_MAX when the result
13 : : * would overflow `size_t`.
14 : : */
15 : 4200 : size_t blocks_to_bytes(const blkcnt_t blocks)
16 : : {
17 [ + + ]: 4200 : if(blocks <= 0)
18 : : {
19 : 22 : return 0;
20 : : }
21 : :
22 : : // Guard against multiplication overflow: if blocks exceeds the safe range,
23 : : // return SIZE_MAX as a saturating upper bound instead of wrapping around
24 [ - + ]: 4178 : if((size_t)blocks > SIZE_MAX / POSIX_STAT_BLOCK_BYTES)
25 : : {
26 : 0 : return SIZE_MAX;
27 : : }
28 : :
29 : 4178 : return (size_t)blocks * POSIX_STAT_BLOCK_BYTES;
30 : : }
|