diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c index 0495a690ea85..bfe456d8fd96 100644 --- a/sbin/fdisk/fdisk.c +++ b/sbin/fdisk/fdisk.c @@ -62,7 +62,7 @@ static char lbuf[LBUF]; * Created. */ -#define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp +#define Decimal(str, ans, tmp, size) if (decimal(str, &tmp, ans, size)) ans = tmp #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs) @@ -247,7 +247,7 @@ static int get_params(void); static int read_s0(void); static int write_s0(void); static int ok(const char *str); -static int decimal(const char *str, int *num, int deflt); +static int decimal(const char *str, int *num, int deflt, int size); static int read_config(char *config_file); static void reset_boot(void); static int sanitize_partition(struct dos_partition *); @@ -572,9 +572,9 @@ change_part(int i) } do { - Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp); - Decimal("start", partp->dp_start, tmp); - Decimal("size", partp->dp_size, tmp); + Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp, sizeof(partp->dp_typ)); + Decimal("start", partp->dp_start, tmp, sizeof(partp->dp_start)); + Decimal("size", partp->dp_size, tmp, sizeof(partp->dp_size)); if (!sanitize_partition(partp)) { warnx("ERROR: failed to adjust; setting sysid to 0"); partp->dp_typ = 0; @@ -586,9 +586,9 @@ change_part(int i) tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect); thd = partp->dp_shd; tsec = DPSECT(partp->dp_ssect); - Decimal("beginning cylinder", tcyl, tmp); - Decimal("beginning head", thd, tmp); - Decimal("beginning sector", tsec, tmp); + Decimal("beginning cylinder", tcyl, tmp, sizeof(partp->dp_scyl)); + Decimal("beginning head", thd, tmp, sizeof(partp->dp_shd)); + Decimal("beginning sector", tsec, tmp, sizeof(partp->dp_ssect)); partp->dp_scyl = DOSCYL(tcyl); partp->dp_ssect = DOSSECT(tsec,tcyl); partp->dp_shd = thd; @@ -596,9 +596,9 @@ change_part(int i) tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect); thd = partp->dp_ehd; tsec = DPSECT(partp->dp_esect); - Decimal("ending cylinder", tcyl, tmp); - Decimal("ending head", thd, tmp); - Decimal("ending sector", tsec, tmp); + Decimal("ending cylinder", tcyl, tmp, sizeof(partp->dp_ecyl)); + Decimal("ending head", thd, tmp, sizeof(partp->dp_ehd)); + Decimal("ending sector", tsec, tmp, sizeof(partp->dp_esect)); partp->dp_ecyl = DOSCYL(tcyl); partp->dp_esect = DOSSECT(tsec,tcyl); partp->dp_ehd = thd; @@ -647,7 +647,7 @@ change_active(int which) setactive: do { new = active; - Decimal("active partition", new, tmp); + Decimal("active partition", new, tmp, 0); if (new < 1 || new > 4) { printf("Active partition number must be in range 1-4." " Try again.\n"); @@ -677,9 +677,9 @@ get_params_to_use() { do { - Decimal("BIOS's idea of #cylinders", dos_cyls, tmp); - Decimal("BIOS's idea of #heads", dos_heads, tmp); - Decimal("BIOS's idea of #sectors", dos_sectors, tmp); + Decimal("BIOS's idea of #cylinders", dos_cyls, tmp, 0); + Decimal("BIOS's idea of #heads", dos_heads, tmp, 0); + Decimal("BIOS's idea of #sectors", dos_sectors, tmp, 0); dos_cylsecs = dos_heads * dos_sectors; print_params(); } @@ -915,11 +915,16 @@ ok(const char *str) } static int -decimal(const char *str, int *num, int deflt) +decimal(const char *str, int *num, int deflt, int size) { - int acc = 0, c; + long long acc = 0, maxval; + int c; char *cp; + if (size == 0) { + size = sizeof(*num); + } + maxval = (long long)1 << (size * 8); while (1) { printf("Supply a decimal value for \"%s\" [%d] ", str, deflt); fflush(stdout); @@ -935,14 +940,20 @@ decimal(const char *str, int *num, int deflt) if (!c) return 0; while ((c = *cp++)) { - if (c <= '9' && c >= '0') - acc = acc * 10 + c - '0'; - else + if (c <= '9' && c >= '0') { + if (acc < maxval) + acc = acc * 10 + c - '0'; + } else break; } if (c == ' ' || c == '\t') while ((c = *cp) && (c == ' ' || c == '\t')) cp++; if (!c) { + if (acc >= maxval) { + acc = maxval - 1; + printf("%s is too big, it will be truncated to %lld\n", + lbuf, acc); + } *num = acc; return 1; } else