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);
#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) {

View 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);

View 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)

View 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");