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 /util-linux/fdisk.c | |
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>
Diffstat (limited to 'util-linux/fdisk.c')
-rw-r--r-- | util-linux/fdisk.c | 31 |
1 files changed, 13 insertions, 18 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) { |