fdisk: avoid overflow in "mega/gigabytes" calculation, code shrink
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 4 Oct 2019 14:45:04 +0000 (16:45 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 4 Oct 2019 14:45:04 +0000 (16:45 +0200)
function                                             old     new   delta
list_disk_geometry                                   175     145     -30

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
util-linux/fdisk.c
util-linux/fdisk_osf.c
util-linux/fdisk_sgi.c
util-linux/fdisk_sun.c

index f28d4fdd28972b0d1054e47b9977550faccb2474..076c5ca577e955a6b958b20307502d795e193ab6 100644 (file)
@@ -299,9 +299,6 @@ static int get_boot(enum action what);
 static int get_boot(void);
 #endif
 
-#define PLURAL   0
-#define SINGULAR 1
-
 static sector_t get_start_sect(const struct partition *p);
 static sector_t get_nr_sects(const struct partition *p);
 
@@ -597,12 +594,10 @@ get_part_table(int i)
        return ptes[i].part_table;
 }
 
-static const char *
-str_units(int n)
-{      /* n==1: use singular */
-       if (n == 1)
-               return display_in_cyl_units ? "cylinder" : "sector";
-       return display_in_cyl_units ? "cylinders" : "sectors";
+static ALWAYS_INLINE const char *
+str_units(void)
+{
+       return display_in_cyl_units ? "cylinder" : "sector";
 }
 
 static int
@@ -1778,8 +1773,8 @@ change_units(void)
 {
        display_in_cyl_units = !display_in_cyl_units;
        update_units();
-       printf("Changing display/entry units to %s\n",
-               str_units(PLURAL));
+       printf("Changing display/entry units to %ss\n",
+               str_units());
 }
 
 static void
@@ -2030,8 +2025,7 @@ check_consistency(const struct partition *p, int partition)
 static void
 list_disk_geometry(void)
 {
-       ullong bytes = ((ullong)total_number_of_sectors << 9);
-       ullong xbytes = bytes / (1024*1024);
+       ullong xbytes = total_number_of_sectors / (1024*1024 / 512);
        char x = 'M';
 
        if (xbytes >= 10000) {
@@ -2041,11 +2035,12 @@ list_disk_geometry(void)
        }
        printf("Disk %s: %llu %cB, %llu bytes, %"SECT_FMT"u sectors\n"
                "%u cylinders, %u heads, %u sectors/track\n"
-               "Units: %s of %u * %u = %u bytes\n\n",
+               "Units: %ss of %u * %u = %u bytes\n"
+               "\n",
                disk_device, xbytes, x,
-               bytes, total_number_of_sectors,
+               ((ullong)total_number_of_sectors * 512), total_number_of_sectors,
                g_cylinders, g_heads, g_sectors,
-               str_units(PLURAL),
+               str_units(),
                units_per_sector, sector_size, units_per_sector * sector_size
        );
 }
@@ -2486,7 +2481,7 @@ add_partition(int n, int sys)
                for (i = 0; i < g_partitions; i++)
                        first[i] = (cround(first[i]) - 1) * units_per_sector;
 
-       snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
+       snprintf(mesg, sizeof(mesg), "First %s", str_units());
        do {
                temp = start;
                for (i = 0; i < g_partitions; i++) {
@@ -2548,7 +2543,7 @@ add_partition(int n, int sys)
        } else {
                snprintf(mesg, sizeof(mesg),
                         "Last %s or +size{,K,M,G,T}",
-                        str_units(SINGULAR)
+                        str_units()
                );
                stop = read_int(cround(start), cround(limit), cround(limit), cround(start), mesg);
                if (display_in_cyl_units) {
index 1328c1fcdcf374b9ec64eb15d263b433fc8c51dc..92180b2bc91a78b1d3befc7357d62d8996dbd059 100644 (file)
@@ -470,7 +470,7 @@ xbsd_new_part(void)
        end = xbsd_dlabel.d_secperunit - 1;
 #endif
 
-       snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
+       snprintf(mesg, sizeof(mesg), "First %s", str_units());
        begin = read_int(bsd_cround(begin), bsd_cround(begin), bsd_cround(end),
                0, mesg);
 
@@ -478,7 +478,7 @@ xbsd_new_part(void)
                begin = (begin - 1) * xbsd_dlabel.d_secpercyl;
 
        snprintf(mesg, sizeof(mesg), "Last %s or +size or +sizeM or +sizeK",
-               str_units(SINGULAR));
+               str_units());
        end = read_int(bsd_cround(begin), bsd_cround(end), bsd_cround(end),
                bsd_cround(begin), mesg);
 
index 0e5491a19c14e4f53febea1f3c757cbf29bda024..c90c801e2990b2b4c9010644dba63241d3188d0a 100644 (file)
@@ -295,19 +295,19 @@ sgi_list_table(int xtra)
                        "%u cylinders, %u physical cylinders\n"
                        "%u extra sects/cyl, interleave %u:1\n"
                        "%s\n"
-                       "Units = %s of %u * 512 bytes\n\n",
+                       "Units = %ss of %u * 512 bytes\n\n",
                        disk_device, g_heads, g_sectors, g_cylinders,
                        SGI_SSWAP16(sgiparam.pcylcount),
                        SGI_SSWAP16(sgiparam.sparecyl),
                        SGI_SSWAP16(sgiparam.ilfact),
                        (char *)sgilabel,
-                       str_units(PLURAL), units_per_sector);
+                       str_units(), units_per_sector);
        } else {
                printf("\nDisk %s (SGI disk label): "
                        "%u heads, %u sectors, %u cylinders\n"
-                       "Units = %s of %u * 512 bytes\n\n",
+                       "Units = %ss of %u * 512 bytes\n\n",
                        disk_device, g_heads, g_sectors, g_cylinders,
-                       str_units(PLURAL), units_per_sector );
+                       str_units(), units_per_sector );
        }
 
        w = strlen(disk_device);
@@ -720,7 +720,7 @@ sgi_add_partition(int n, int sys)
                printf("You got a partition overlap on the disk. Fix it first!\n");
                return;
        }
-       snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
+       snprintf(mesg, sizeof(mesg), "First %s", str_units());
        while (1) {
                if (sys == SGI_VOLUME) {
                        last = sgi_get_lastblock();
@@ -746,7 +746,7 @@ sgi_add_partition(int n, int sys)
                printf("You will get a partition overlap on the disk. "
                                "Fix it first!\n");
        }
-       snprintf(mesg, sizeof(mesg), " Last %s", str_units(SINGULAR));
+       snprintf(mesg, sizeof(mesg), " Last %s", str_units());
        last = read_int(scround(first), scround(last)-1, scround(last)-1,
                        scround(first), mesg)+1;
        if (display_in_cyl_units)
index 3697a69b95dda6d8b013ded3c7c49ddb0f580248..29d7c283a8955de4b4d088ce3688af61b9c2602d 100644 (file)
@@ -491,7 +491,7 @@ add_sun_partition(int n, int sys)
                        return;
                }
        }
-       snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
+       snprintf(mesg, sizeof(mesg), "First %s", str_units());
        while (1) {
                if (whole_disk)
                        first = read_int(0, 0, 0, 0, mesg);
@@ -546,7 +546,7 @@ and is of type 'Whole disk'\n");
        }
        snprintf(mesg, sizeof(mesg),
                "Last %s or +size or +sizeM or +sizeK",
-               str_units(SINGULAR));
+               str_units());
        if (whole_disk)
                last = read_int(scround(stop2), scround(stop2), scround(stop2),
                                0, mesg);
@@ -567,8 +567,8 @@ and is of type 'Whole disk'\n");
 "You haven't covered the whole disk with the 3rd partition,\n"
 "but your value %u %s covers some other partition.\n"
 "Your entry has been changed to %u %s\n",
-                               scround(last), str_units(SINGULAR),
-                               scround(stop), str_units(SINGULAR));
+                               scround(last), str_units(),
+                               scround(stop), str_units());
                        last = stop;
                }
        } else if (!whole_disk && last > stop)
@@ -636,20 +636,20 @@ sun_list_table(int xtra)
                "%u cylinders, %u alternate cylinders, %u physical cylinders\n"
                "%u extra sects/cyl, interleave %u:1\n"
                "%s\n"
-               "Units = %s of %u * 512 bytes\n\n",
+               "Units = %ss of %u * 512 bytes\n\n",
                        disk_device, g_heads, g_sectors, SUN_SSWAP16(sunlabel->rspeed),
                        g_cylinders, SUN_SSWAP16(sunlabel->nacyl),
                        SUN_SSWAP16(sunlabel->pcylcount),
                        SUN_SSWAP16(sunlabel->sparecyl),
                        SUN_SSWAP16(sunlabel->ilfact),
                        (char *)sunlabel,
-                       str_units(PLURAL), units_per_sector);
+                       str_units(), units_per_sector);
        else
                printf(
        "\nDisk %s (Sun disk label): %u heads, %u sectors, %u cylinders\n"
-       "Units = %s of %u * 512 bytes\n\n",
+       "Units = %ss of %u * 512 bytes\n\n",
                        disk_device, g_heads, g_sectors, g_cylinders,
-                       str_units(PLURAL), units_per_sector);
+                       str_units(), units_per_sector);
 
        printf("%*s Flag    Start       End    Blocks   Id  System\n",
                w + 1, "Device");