sbin/fsck_msdosfs: Do not overflow when calculating file system size
authorTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Tue, 28 Apr 2020 18:10:08 +0000 (03:10 +0900)
committerTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Tue, 28 Apr 2020 18:10:08 +0000 (03:10 +0900)
from freebsd/freebsd@2ccadef77b81e7f18dd6bc4380357d6c6a797c5a

sbin/fsck_msdosfs/check.c

index d7054ff..045f085 100644 (file)
@@ -44,6 +44,8 @@ checkfilesys(const char *fname)
        int finish_dosdirsection=0;
        int mod = 0;
        int ret = 8;
+       int64_t freebytes;
+       int64_t badbytes;
 
        rdonly = alwaysno;
        if (!preen)
@@ -112,37 +114,33 @@ checkfilesys(const char *fname)
                        mod |= FSERROR;
        }
 
+       freebytes = (int64_t)boot.NumFree * boot.ClusterSize;
+       badbytes = (int64_t)boot.NumBad * boot.ClusterSize;
+
 #if 1
        char freestr[7], badstr[7];
 
-       int64_t freebytes = boot.NumFree * boot.ClusterSize;
        humanize_number(freestr, sizeof(freestr), freebytes, "",
            HN_AUTOSCALE, HN_DECIMAL | HN_IEC_PREFIXES);
        if (boot.NumBad) {
-               int64_t badbytes = boot.NumBad * boot.ClusterSize;
-
                humanize_number(badstr, sizeof(badstr), badbytes, "",
                    HN_AUTOSCALE, HN_B | HN_DECIMAL | HN_IEC_PREFIXES);
 
                pwarn("%d files, %sB free (%d clusters), %sB bad (%d clusters)\n",
-                     boot.NumFiles,
-                     freestr, boot.NumFree,
+                     boot.NumFiles, freestr, boot.NumFree,
                      badstr, boot.NumBad);
        } else {
                pwarn("%d files, %sB free (%d clusters)\n",
-                     boot.NumFiles,
-                     freestr, boot.NumFree);
+                     boot.NumFiles, freestr, boot.NumFree);
        }
 #else
        if (boot.NumBad)
-               pwarn("%d files, %d KiB free (%d clusters), %d KiB bad (%d clusters)\n",
-                     boot.NumFiles,
-                     boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
-                     boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
+               pwarn("%d files, %jd KiB free (%d clusters), %jd KiB bad (%d clusters)\n",
+                     boot.NumFiles, (intmax_t)freebytes / 1024, boot.NumFree,
+                     (intmax_t)badbytes / 1024, boot.NumBad);
        else
-               pwarn("%d files, %d KiB free (%d clusters)\n",
-                     boot.NumFiles,
-                     boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
+               pwarn("%d files, %jd KiB free (%d clusters)\n",
+                     boot.NumFiles, (intmax_t)freebytes / 1024, boot.NumFree);
 #endif
 
        if (mod && (mod & FSERROR) == 0) {