The first is a pretty trivial change for clarity and correctness.
The second probably isn't the best way to solve the problem long-term,
but prevents a div-by-zero two lines later. I may have been affected by
it when fscking a /dev/zero'd partition.
Found with the Clang static analyzer (and the aforementioned arithmetic
exception).
Index: setup.c
===================================================================
RCS file: /cvs/src/sbin/fsck_ext2fs/setup.c,v
retrieving revision 1.26
diff -u -p -r1.26 setup.c
--- setup.c 20 Aug 2015 22:02:20 -0000 1.26
+++ setup.c 7 Sep 2015 03:51:05 -0000
@@ -212,7 +212,7 @@ setup(char *dev)
(unsigned)(maxino + 1));
goto badsblabel;
}
- typemap = calloc((unsigned)(maxino + 1), sizeof(char));
+ typemap = calloc((unsigned)(maxino + 1), sizeof(u_char));
if (typemap == NULL) {
printf("cannot alloc %u bytes for typemap\n",
(unsigned)(maxino + 1));
@@ -449,6 +449,10 @@ calcsb(char *dev, int devfd, struct m_ex
}
memset(fs, 0, sizeof(struct m_ext2fs));
fs->e2fs_bsize = DISKLABELV1_FFS_FSIZE(pp->p_fragblock); /* XXX */
+ if (fs->e2fs_bsize == 0) {
+ pfatal("%s: BLOCK SIZE DETERMINED TO BE ZERO\n", dev);
+ return (0);
+ }
fs->e2fs.e2fs_log_bsize = fs->e2fs_bsize / 1024;
fs->e2fs.e2fs_bcount = (pp->p_size * DEV_BSIZE) / fs->e2fs_bsize;
fs->e2fs.e2fs_first_dblock = (fs->e2fs.e2fs_log_bsize == 0) ? 1 : 0;