fdisk: avoid overflow in "mega/gigabytes" calculation, code shrink

function                                             old     new   delta
list_disk_geometry                                   175     145     -30

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2019-10-04 16:45:04 +02:00
parent 65741d004e
commit d8e4ce0503
4 changed files with 29 additions and 34 deletions

View File

@ -299,9 +299,6 @@ static int get_boot(enum action what);
static int get_boot(void); static int get_boot(void);
#endif #endif
#define PLURAL 0
#define SINGULAR 1
static sector_t get_start_sect(const struct partition *p); static sector_t get_start_sect(const struct partition *p);
static sector_t get_nr_sects(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; return ptes[i].part_table;
} }
static const char * static ALWAYS_INLINE const char *
str_units(int n) str_units(void)
{ /* n==1: use singular */ {
if (n == 1)
return display_in_cyl_units ? "cylinder" : "sector"; return display_in_cyl_units ? "cylinder" : "sector";
return display_in_cyl_units ? "cylinders" : "sectors";
} }
static int static int
@ -1778,8 +1773,8 @@ change_units(void)
{ {
display_in_cyl_units = !display_in_cyl_units; display_in_cyl_units = !display_in_cyl_units;
update_units(); update_units();
printf("Changing display/entry units to %s\n", printf("Changing display/entry units to %ss\n",
str_units(PLURAL)); str_units());
} }
static void static void
@ -2030,8 +2025,7 @@ check_consistency(const struct partition *p, int partition)
static void static void
list_disk_geometry(void) list_disk_geometry(void)
{ {
ullong bytes = ((ullong)total_number_of_sectors << 9); ullong xbytes = total_number_of_sectors / (1024*1024 / 512);
ullong xbytes = bytes / (1024*1024);
char x = 'M'; char x = 'M';
if (xbytes >= 10000) { if (xbytes >= 10000) {
@ -2041,11 +2035,12 @@ list_disk_geometry(void)
} }
printf("Disk %s: %llu %cB, %llu bytes, %"SECT_FMT"u sectors\n" printf("Disk %s: %llu %cB, %llu bytes, %"SECT_FMT"u sectors\n"
"%u cylinders, %u heads, %u sectors/track\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, 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, g_cylinders, g_heads, g_sectors,
str_units(PLURAL), str_units(),
units_per_sector, sector_size, units_per_sector * sector_size 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++) for (i = 0; i < g_partitions; i++)
first[i] = (cround(first[i]) - 1) * units_per_sector; 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 { do {
temp = start; temp = start;
for (i = 0; i < g_partitions; i++) { for (i = 0; i < g_partitions; i++) {
@ -2548,7 +2543,7 @@ add_partition(int n, int sys)
} else { } else {
snprintf(mesg, sizeof(mesg), snprintf(mesg, sizeof(mesg),
"Last %s or +size{,K,M,G,T}", "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); stop = read_int(cround(start), cround(limit), cround(limit), cround(start), mesg);
if (display_in_cyl_units) { if (display_in_cyl_units) {

View File

@ -470,7 +470,7 @@ xbsd_new_part(void)
end = xbsd_dlabel.d_secperunit - 1; end = xbsd_dlabel.d_secperunit - 1;
#endif #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), begin = read_int(bsd_cround(begin), bsd_cround(begin), bsd_cround(end),
0, mesg); 0, mesg);
@ -478,7 +478,7 @@ xbsd_new_part(void)
begin = (begin - 1) * xbsd_dlabel.d_secpercyl; begin = (begin - 1) * xbsd_dlabel.d_secpercyl;
snprintf(mesg, sizeof(mesg), "Last %s or +size or +sizeM or +sizeK", 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), end = read_int(bsd_cround(begin), bsd_cround(end), bsd_cround(end),
bsd_cround(begin), mesg); bsd_cround(begin), mesg);

View File

@ -295,19 +295,19 @@ sgi_list_table(int xtra)
"%u cylinders, %u physical cylinders\n" "%u cylinders, %u physical cylinders\n"
"%u extra sects/cyl, interleave %u:1\n" "%u extra sects/cyl, interleave %u:1\n"
"%s\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, disk_device, g_heads, g_sectors, g_cylinders,
SGI_SSWAP16(sgiparam.pcylcount), SGI_SSWAP16(sgiparam.pcylcount),
SGI_SSWAP16(sgiparam.sparecyl), SGI_SSWAP16(sgiparam.sparecyl),
SGI_SSWAP16(sgiparam.ilfact), SGI_SSWAP16(sgiparam.ilfact),
(char *)sgilabel, (char *)sgilabel,
str_units(PLURAL), units_per_sector); str_units(), units_per_sector);
} else { } else {
printf("\nDisk %s (SGI disk label): " printf("\nDisk %s (SGI disk label): "
"%u heads, %u sectors, %u cylinders\n" "%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, disk_device, g_heads, g_sectors, g_cylinders,
str_units(PLURAL), units_per_sector ); str_units(), units_per_sector );
} }
w = strlen(disk_device); 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"); printf("You got a partition overlap on the disk. Fix it first!\n");
return; return;
} }
snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR)); snprintf(mesg, sizeof(mesg), "First %s", str_units());
while (1) { while (1) {
if (sys == SGI_VOLUME) { if (sys == SGI_VOLUME) {
last = sgi_get_lastblock(); 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. " printf("You will get a partition overlap on the disk. "
"Fix it first!\n"); "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, last = read_int(scround(first), scround(last)-1, scround(last)-1,
scround(first), mesg)+1; scround(first), mesg)+1;
if (display_in_cyl_units) if (display_in_cyl_units)

View File

@ -491,7 +491,7 @@ add_sun_partition(int n, int sys)
return; return;
} }
} }
snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR)); snprintf(mesg, sizeof(mesg), "First %s", str_units());
while (1) { while (1) {
if (whole_disk) if (whole_disk)
first = read_int(0, 0, 0, 0, mesg); first = read_int(0, 0, 0, 0, mesg);
@ -546,7 +546,7 @@ and is of type 'Whole disk'\n");
} }
snprintf(mesg, sizeof(mesg), snprintf(mesg, sizeof(mesg),
"Last %s or +size or +sizeM or +sizeK", "Last %s or +size or +sizeM or +sizeK",
str_units(SINGULAR)); str_units());
if (whole_disk) if (whole_disk)
last = read_int(scround(stop2), scround(stop2), scround(stop2), last = read_int(scround(stop2), scround(stop2), scround(stop2),
0, mesg); 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" "You haven't covered the whole disk with the 3rd partition,\n"
"but your value %u %s covers some other partition.\n" "but your value %u %s covers some other partition.\n"
"Your entry has been changed to %u %s\n", "Your entry has been changed to %u %s\n",
scround(last), str_units(SINGULAR), scround(last), str_units(),
scround(stop), str_units(SINGULAR)); scround(stop), str_units());
last = stop; last = stop;
} }
} else if (!whole_disk && 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 cylinders, %u alternate cylinders, %u physical cylinders\n"
"%u extra sects/cyl, interleave %u:1\n" "%u extra sects/cyl, interleave %u:1\n"
"%s\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), disk_device, g_heads, g_sectors, SUN_SSWAP16(sunlabel->rspeed),
g_cylinders, SUN_SSWAP16(sunlabel->nacyl), g_cylinders, SUN_SSWAP16(sunlabel->nacyl),
SUN_SSWAP16(sunlabel->pcylcount), SUN_SSWAP16(sunlabel->pcylcount),
SUN_SSWAP16(sunlabel->sparecyl), SUN_SSWAP16(sunlabel->sparecyl),
SUN_SSWAP16(sunlabel->ilfact), SUN_SSWAP16(sunlabel->ilfact),
(char *)sunlabel, (char *)sunlabel,
str_units(PLURAL), units_per_sector); str_units(), units_per_sector);
else else
printf( printf(
"\nDisk %s (Sun disk label): %u heads, %u sectors, %u cylinders\n" "\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, 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", printf("%*s Flag Start End Blocks Id System\n",
w + 1, "Device"); w + 1, "Device");