Whamcloud - gitweb
badblocks: fix mis-printed error from block size check
authorCorey Hickey <bugfood-c@fatooh.org>
Mon, 24 Jan 2022 01:39:33 +0000 (17:39 -0800)
committerTheodore Ts'o <tytso@mit.edu>
Wed, 1 Feb 2023 16:37:18 +0000 (11:37 -0500)
block_size is parsed as an unsigned int from parse_uint(), so retain it
as such until _after_ it has been constrained to a size within INT_MAX.

Lower level code still requires this to be an int, so cast to int for
anything below main().

Before:
$ misc/badblocks -w -b 4294967295 -c 1 /tmp/testfile.bin
misc/badblocks: Invalid block size: -1

After:
$ misc/badblocks -w -b 4294967295 -c 1 /tmp/testfile.bin
misc/badblocks: Invalid block size: 4294967295

Signed-off-by: Corey Hickey <bugfood-c@fatooh.org>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
misc/badblocks.c

index 85a53b0..e640ead 100644 (file)
@@ -1055,7 +1055,7 @@ int main (int argc, char ** argv)
        char * input_file = NULL;
        char * output_file = NULL;
        FILE * in = NULL;
-       int block_size = 1024;
+       unsigned int block_size = 1024;
        unsigned int blocks_at_once = 64;
        blk64_t last_block, first_block;
        int num_passes = 0;
@@ -1205,9 +1205,9 @@ int main (int argc, char ** argv)
                        exit(1);
                }
        }
-       if ((block_size <= 0) || (block_size > (1 << 24)) ||
+       if ((block_size == 0) || (block_size > (1 << 24)) ||
            (block_size & (block_size - 1))) {
-               com_err(program_name, 0, _("Invalid block size: %d\n"),
+               com_err(program_name, 0, _("Invalid block size: %u\n"),
                        block_size);
                exit(1);
        }
@@ -1223,7 +1223,7 @@ int main (int argc, char ** argv)
        device_name = argv[optind++];
        if (optind > argc - 1) {
                errcode = ext2fs_get_device_size2(device_name,
-                                                block_size,
+                                                (int) block_size,
                                                 &last_block);
                if (errcode == EXT2_ET_UNIMPLEMENTED) {
                        com_err(program_name, 0, "%s",
@@ -1353,7 +1353,7 @@ int main (int argc, char ** argv)
        do {
                unsigned int bb_count;
 
-               bb_count = test_func(dev, last_block, block_size,
+               bb_count = test_func(dev, last_block, (int) block_size,
                                     first_block, blocks_at_once);
                if (bb_count)
                        passes_clean = 0;