Skip to content

Commit 14a8f3f

Browse files
committed
shred:add checks for block sizes
This commit adds checks for block size constants. Misconfigured sizes could lead to overflows or performance issues. While such cases are expected to be rare or unlikely, it's better to be notified of them early.
1 parent 24d88d7 commit 14a8f3f

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/uu/shred/src/shred.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,17 @@ fn wipe_file(
514514
}
515515

516516
fn split_on_blocks(file_size: u64, exact: bool) -> (u64, u64) {
517+
// OPTIMAL_IO_BLOCK_SIZE must not exceed BLOCK_SIZE. Violating this may cause overflows due
518+
// to alignment or performance issues.This kind of misconfiguration is
519+
// highly unlikely but would indicate a serious error.
520+
const _: () = assert!(OPTIMAL_IO_BLOCK_SIZE <= BLOCK_SIZE);
521+
517522
let file_size = if exact {
518523
file_size
519524
} else {
520-
// The main idea here is to align the file size to the OPTIMAL_IO_BLOCK_SIZE, and then split it into
521-
// BLOCK_SIZE + remaining bytes. Since the input data is already aligned to N * OPTIMAL_IO_BLOCK_SIZE,
522-
// the output file size will also be aligned and correct.
525+
// The main idea here is to align the file size to the OPTIMAL_IO_BLOCK_SIZE, and then
526+
// split it into BLOCK_SIZE + remaining bytes. Since the input data is already aligned to N
527+
// * OPTIMAL_IO_BLOCK_SIZE, the output file size will also be aligned and correct.
523528
file_size.div_ceil(OPTIMAL_IO_BLOCK_SIZE as u64) * OPTIMAL_IO_BLOCK_SIZE as u64
524529
};
525530
(file_size / BLOCK_SIZE as u64, file_size % BLOCK_SIZE as u64)

0 commit comments

Comments
 (0)