diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2019-10-04 16:45:04 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2019-10-04 16:45:04 +0200 |
commit | d8e4ce05039a89c2e0b41f008d74a83db45f2287 (patch) | |
tree | 147536311a6c19a12b331b02dd6b2819b48fae23 | |
parent | 65741d004ee91c5a710559ae4f1664f25009255e (diff) | |
download | busybox-w32-d8e4ce05039a89c2e0b41f008d74a83db45f2287.tar.gz busybox-w32-d8e4ce05039a89c2e0b41f008d74a83db45f2287.tar.bz2 busybox-w32-d8e4ce05039a89c2e0b41f008d74a83db45f2287.zip |
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>
-rw-r--r-- | util-linux/fdisk.c | 31 | ||||
-rw-r--r-- | util-linux/fdisk_osf.c | 4 | ||||
-rw-r--r-- | util-linux/fdisk_sgi.c | 12 | ||||
-rw-r--r-- | util-linux/fdisk_sun.c | 16 |
4 files changed, 29 insertions, 34 deletions
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c index f28d4fdd2..076c5ca57 100644 --- a/util-linux/fdisk.c +++ b/util-linux/fdisk.c | |||
@@ -299,9 +299,6 @@ static int get_boot(enum action what); | |||
299 | static int get_boot(void); | 299 | static int get_boot(void); |
300 | #endif | 300 | #endif |
301 | 301 | ||
302 | #define PLURAL 0 | ||
303 | #define SINGULAR 1 | ||
304 | |||
305 | static sector_t get_start_sect(const struct partition *p); | 302 | static sector_t get_start_sect(const struct partition *p); |
306 | static sector_t get_nr_sects(const struct partition *p); | 303 | static sector_t get_nr_sects(const struct partition *p); |
307 | 304 | ||
@@ -597,12 +594,10 @@ get_part_table(int i) | |||
597 | return ptes[i].part_table; | 594 | return ptes[i].part_table; |
598 | } | 595 | } |
599 | 596 | ||
600 | static const char * | 597 | static ALWAYS_INLINE const char * |
601 | str_units(int n) | 598 | str_units(void) |
602 | { /* n==1: use singular */ | 599 | { |
603 | if (n == 1) | 600 | return display_in_cyl_units ? "cylinder" : "sector"; |
604 | return display_in_cyl_units ? "cylinder" : "sector"; | ||
605 | return display_in_cyl_units ? "cylinders" : "sectors"; | ||
606 | } | 601 | } |
607 | 602 | ||
608 | static int | 603 | static int |
@@ -1778,8 +1773,8 @@ change_units(void) | |||
1778 | { | 1773 | { |
1779 | display_in_cyl_units = !display_in_cyl_units; | 1774 | display_in_cyl_units = !display_in_cyl_units; |
1780 | update_units(); | 1775 | update_units(); |
1781 | printf("Changing display/entry units to %s\n", | 1776 | printf("Changing display/entry units to %ss\n", |
1782 | str_units(PLURAL)); | 1777 | str_units()); |
1783 | } | 1778 | } |
1784 | 1779 | ||
1785 | static void | 1780 | static void |
@@ -2030,8 +2025,7 @@ check_consistency(const struct partition *p, int partition) | |||
2030 | static void | 2025 | static void |
2031 | list_disk_geometry(void) | 2026 | list_disk_geometry(void) |
2032 | { | 2027 | { |
2033 | ullong bytes = ((ullong)total_number_of_sectors << 9); | 2028 | ullong xbytes = total_number_of_sectors / (1024*1024 / 512); |
2034 | ullong xbytes = bytes / (1024*1024); | ||
2035 | char x = 'M'; | 2029 | char x = 'M'; |
2036 | 2030 | ||
2037 | if (xbytes >= 10000) { | 2031 | if (xbytes >= 10000) { |
@@ -2041,11 +2035,12 @@ list_disk_geometry(void) | |||
2041 | } | 2035 | } |
2042 | printf("Disk %s: %llu %cB, %llu bytes, %"SECT_FMT"u sectors\n" | 2036 | printf("Disk %s: %llu %cB, %llu bytes, %"SECT_FMT"u sectors\n" |
2043 | "%u cylinders, %u heads, %u sectors/track\n" | 2037 | "%u cylinders, %u heads, %u sectors/track\n" |
2044 | "Units: %s of %u * %u = %u bytes\n\n", | 2038 | "Units: %ss of %u * %u = %u bytes\n" |
2039 | "\n", | ||
2045 | disk_device, xbytes, x, | 2040 | disk_device, xbytes, x, |
2046 | bytes, total_number_of_sectors, | 2041 | ((ullong)total_number_of_sectors * 512), total_number_of_sectors, |
2047 | g_cylinders, g_heads, g_sectors, | 2042 | g_cylinders, g_heads, g_sectors, |
2048 | str_units(PLURAL), | 2043 | str_units(), |
2049 | units_per_sector, sector_size, units_per_sector * sector_size | 2044 | units_per_sector, sector_size, units_per_sector * sector_size |
2050 | ); | 2045 | ); |
2051 | } | 2046 | } |
@@ -2486,7 +2481,7 @@ add_partition(int n, int sys) | |||
2486 | for (i = 0; i < g_partitions; i++) | 2481 | for (i = 0; i < g_partitions; i++) |
2487 | first[i] = (cround(first[i]) - 1) * units_per_sector; | 2482 | first[i] = (cround(first[i]) - 1) * units_per_sector; |
2488 | 2483 | ||
2489 | snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR)); | 2484 | snprintf(mesg, sizeof(mesg), "First %s", str_units()); |
2490 | do { | 2485 | do { |
2491 | temp = start; | 2486 | temp = start; |
2492 | for (i = 0; i < g_partitions; i++) { | 2487 | for (i = 0; i < g_partitions; i++) { |
@@ -2548,7 +2543,7 @@ add_partition(int n, int sys) | |||
2548 | } else { | 2543 | } else { |
2549 | snprintf(mesg, sizeof(mesg), | 2544 | snprintf(mesg, sizeof(mesg), |
2550 | "Last %s or +size{,K,M,G,T}", | 2545 | "Last %s or +size{,K,M,G,T}", |
2551 | str_units(SINGULAR) | 2546 | str_units() |
2552 | ); | 2547 | ); |
2553 | stop = read_int(cround(start), cround(limit), cround(limit), cround(start), mesg); | 2548 | stop = read_int(cround(start), cround(limit), cround(limit), cround(start), mesg); |
2554 | if (display_in_cyl_units) { | 2549 | if (display_in_cyl_units) { |
diff --git a/util-linux/fdisk_osf.c b/util-linux/fdisk_osf.c index 1328c1fcd..92180b2bc 100644 --- a/util-linux/fdisk_osf.c +++ b/util-linux/fdisk_osf.c | |||
@@ -470,7 +470,7 @@ xbsd_new_part(void) | |||
470 | end = xbsd_dlabel.d_secperunit - 1; | 470 | end = xbsd_dlabel.d_secperunit - 1; |
471 | #endif | 471 | #endif |
472 | 472 | ||
473 | snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR)); | 473 | snprintf(mesg, sizeof(mesg), "First %s", str_units()); |
474 | begin = read_int(bsd_cround(begin), bsd_cround(begin), bsd_cround(end), | 474 | begin = read_int(bsd_cround(begin), bsd_cround(begin), bsd_cround(end), |
475 | 0, mesg); | 475 | 0, mesg); |
476 | 476 | ||
@@ -478,7 +478,7 @@ xbsd_new_part(void) | |||
478 | begin = (begin - 1) * xbsd_dlabel.d_secpercyl; | 478 | begin = (begin - 1) * xbsd_dlabel.d_secpercyl; |
479 | 479 | ||
480 | snprintf(mesg, sizeof(mesg), "Last %s or +size or +sizeM or +sizeK", | 480 | snprintf(mesg, sizeof(mesg), "Last %s or +size or +sizeM or +sizeK", |
481 | str_units(SINGULAR)); | 481 | str_units()); |
482 | end = read_int(bsd_cround(begin), bsd_cround(end), bsd_cround(end), | 482 | end = read_int(bsd_cround(begin), bsd_cround(end), bsd_cround(end), |
483 | bsd_cround(begin), mesg); | 483 | bsd_cround(begin), mesg); |
484 | 484 | ||
diff --git a/util-linux/fdisk_sgi.c b/util-linux/fdisk_sgi.c index 0e5491a19..c90c801e2 100644 --- a/util-linux/fdisk_sgi.c +++ b/util-linux/fdisk_sgi.c | |||
@@ -295,19 +295,19 @@ sgi_list_table(int xtra) | |||
295 | "%u cylinders, %u physical cylinders\n" | 295 | "%u cylinders, %u physical cylinders\n" |
296 | "%u extra sects/cyl, interleave %u:1\n" | 296 | "%u extra sects/cyl, interleave %u:1\n" |
297 | "%s\n" | 297 | "%s\n" |
298 | "Units = %s of %u * 512 bytes\n\n", | 298 | "Units = %ss of %u * 512 bytes\n\n", |
299 | disk_device, g_heads, g_sectors, g_cylinders, | 299 | disk_device, g_heads, g_sectors, g_cylinders, |
300 | SGI_SSWAP16(sgiparam.pcylcount), | 300 | SGI_SSWAP16(sgiparam.pcylcount), |
301 | SGI_SSWAP16(sgiparam.sparecyl), | 301 | SGI_SSWAP16(sgiparam.sparecyl), |
302 | SGI_SSWAP16(sgiparam.ilfact), | 302 | SGI_SSWAP16(sgiparam.ilfact), |
303 | (char *)sgilabel, | 303 | (char *)sgilabel, |
304 | str_units(PLURAL), units_per_sector); | 304 | str_units(), units_per_sector); |
305 | } else { | 305 | } else { |
306 | printf("\nDisk %s (SGI disk label): " | 306 | printf("\nDisk %s (SGI disk label): " |
307 | "%u heads, %u sectors, %u cylinders\n" | 307 | "%u heads, %u sectors, %u cylinders\n" |
308 | "Units = %s of %u * 512 bytes\n\n", | 308 | "Units = %ss of %u * 512 bytes\n\n", |
309 | disk_device, g_heads, g_sectors, g_cylinders, | 309 | disk_device, g_heads, g_sectors, g_cylinders, |
310 | str_units(PLURAL), units_per_sector ); | 310 | str_units(), units_per_sector ); |
311 | } | 311 | } |
312 | 312 | ||
313 | w = strlen(disk_device); | 313 | w = strlen(disk_device); |
@@ -720,7 +720,7 @@ sgi_add_partition(int n, int sys) | |||
720 | printf("You got a partition overlap on the disk. Fix it first!\n"); | 720 | printf("You got a partition overlap on the disk. Fix it first!\n"); |
721 | return; | 721 | return; |
722 | } | 722 | } |
723 | snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR)); | 723 | snprintf(mesg, sizeof(mesg), "First %s", str_units()); |
724 | while (1) { | 724 | while (1) { |
725 | if (sys == SGI_VOLUME) { | 725 | if (sys == SGI_VOLUME) { |
726 | last = sgi_get_lastblock(); | 726 | last = sgi_get_lastblock(); |
@@ -746,7 +746,7 @@ sgi_add_partition(int n, int sys) | |||
746 | printf("You will get a partition overlap on the disk. " | 746 | printf("You will get a partition overlap on the disk. " |
747 | "Fix it first!\n"); | 747 | "Fix it first!\n"); |
748 | } | 748 | } |
749 | snprintf(mesg, sizeof(mesg), " Last %s", str_units(SINGULAR)); | 749 | snprintf(mesg, sizeof(mesg), " Last %s", str_units()); |
750 | last = read_int(scround(first), scround(last)-1, scround(last)-1, | 750 | last = read_int(scround(first), scround(last)-1, scround(last)-1, |
751 | scround(first), mesg)+1; | 751 | scround(first), mesg)+1; |
752 | if (display_in_cyl_units) | 752 | if (display_in_cyl_units) |
diff --git a/util-linux/fdisk_sun.c b/util-linux/fdisk_sun.c index 3697a69b9..29d7c283a 100644 --- a/util-linux/fdisk_sun.c +++ b/util-linux/fdisk_sun.c | |||
@@ -491,7 +491,7 @@ add_sun_partition(int n, int sys) | |||
491 | return; | 491 | return; |
492 | } | 492 | } |
493 | } | 493 | } |
494 | snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR)); | 494 | snprintf(mesg, sizeof(mesg), "First %s", str_units()); |
495 | while (1) { | 495 | while (1) { |
496 | if (whole_disk) | 496 | if (whole_disk) |
497 | first = read_int(0, 0, 0, 0, mesg); | 497 | first = read_int(0, 0, 0, 0, mesg); |
@@ -546,7 +546,7 @@ and is of type 'Whole disk'\n"); | |||
546 | } | 546 | } |
547 | snprintf(mesg, sizeof(mesg), | 547 | snprintf(mesg, sizeof(mesg), |
548 | "Last %s or +size or +sizeM or +sizeK", | 548 | "Last %s or +size or +sizeM or +sizeK", |
549 | str_units(SINGULAR)); | 549 | str_units()); |
550 | if (whole_disk) | 550 | if (whole_disk) |
551 | last = read_int(scround(stop2), scround(stop2), scround(stop2), | 551 | last = read_int(scround(stop2), scround(stop2), scround(stop2), |
552 | 0, mesg); | 552 | 0, mesg); |
@@ -567,8 +567,8 @@ and is of type 'Whole disk'\n"); | |||
567 | "You haven't covered the whole disk with the 3rd partition,\n" | 567 | "You haven't covered the whole disk with the 3rd partition,\n" |
568 | "but your value %u %s covers some other partition.\n" | 568 | "but your value %u %s covers some other partition.\n" |
569 | "Your entry has been changed to %u %s\n", | 569 | "Your entry has been changed to %u %s\n", |
570 | scround(last), str_units(SINGULAR), | 570 | scround(last), str_units(), |
571 | scround(stop), str_units(SINGULAR)); | 571 | scround(stop), str_units()); |
572 | last = stop; | 572 | last = stop; |
573 | } | 573 | } |
574 | } else if (!whole_disk && last > stop) | 574 | } else if (!whole_disk && last > stop) |
@@ -636,20 +636,20 @@ sun_list_table(int xtra) | |||
636 | "%u cylinders, %u alternate cylinders, %u physical cylinders\n" | 636 | "%u cylinders, %u alternate cylinders, %u physical cylinders\n" |
637 | "%u extra sects/cyl, interleave %u:1\n" | 637 | "%u extra sects/cyl, interleave %u:1\n" |
638 | "%s\n" | 638 | "%s\n" |
639 | "Units = %s of %u * 512 bytes\n\n", | 639 | "Units = %ss of %u * 512 bytes\n\n", |
640 | disk_device, g_heads, g_sectors, SUN_SSWAP16(sunlabel->rspeed), | 640 | disk_device, g_heads, g_sectors, SUN_SSWAP16(sunlabel->rspeed), |
641 | g_cylinders, SUN_SSWAP16(sunlabel->nacyl), | 641 | g_cylinders, SUN_SSWAP16(sunlabel->nacyl), |
642 | SUN_SSWAP16(sunlabel->pcylcount), | 642 | SUN_SSWAP16(sunlabel->pcylcount), |
643 | SUN_SSWAP16(sunlabel->sparecyl), | 643 | SUN_SSWAP16(sunlabel->sparecyl), |
644 | SUN_SSWAP16(sunlabel->ilfact), | 644 | SUN_SSWAP16(sunlabel->ilfact), |
645 | (char *)sunlabel, | 645 | (char *)sunlabel, |
646 | str_units(PLURAL), units_per_sector); | 646 | str_units(), units_per_sector); |
647 | else | 647 | else |
648 | printf( | 648 | printf( |
649 | "\nDisk %s (Sun disk label): %u heads, %u sectors, %u cylinders\n" | 649 | "\nDisk %s (Sun disk label): %u heads, %u sectors, %u cylinders\n" |
650 | "Units = %s of %u * 512 bytes\n\n", | 650 | "Units = %ss of %u * 512 bytes\n\n", |
651 | disk_device, g_heads, g_sectors, g_cylinders, | 651 | disk_device, g_heads, g_sectors, g_cylinders, |
652 | str_units(PLURAL), units_per_sector); | 652 | str_units(), units_per_sector); |
653 | 653 | ||
654 | printf("%*s Flag Start End Blocks Id System\n", | 654 | printf("%*s Flag Start End Blocks Id System\n", |
655 | w + 1, "Device"); | 655 | w + 1, "Device"); |