From owner-svn-src-all@FreeBSD.ORG Sat Jan 22 05:21:21 2011 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 402FB1065672; Sat, 22 Jan 2011 05:21:21 +0000 (UTC) (envelope-from sobomax@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 2EC3B8FC0A; Sat, 22 Jan 2011 05:21:21 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id p0M5LL61081186; Sat, 22 Jan 2011 05:21:21 GMT (envelope-from sobomax@svn.freebsd.org) Received: (from sobomax@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id p0M5LLc7081184; Sat, 22 Jan 2011 05:21:21 GMT (envelope-from sobomax@svn.freebsd.org) Message-Id: <201101220521.p0M5LLc7081184@svn.freebsd.org> From: Maxim Sobolev Date: Sat, 22 Jan 2011 05:21:21 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r217714 - head/sbin/fdisk X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 22 Jan 2011 05:21:21 -0000 Author: sobomax Date: Sat Jan 22 05:21:20 2011 New Revision: 217714 URL: http://svn.freebsd.org/changeset/base/217714 Log: Warn user when value entered is greated than the amount supported by the MBR for the given parameter and set that parameter to the maximum value instead of just truncating the most significant part silently. Could happen for example if the capacity of the device is more than 2TB, so that the number of sectors is greater than 2Mib. MFC after: 1 month Modified: head/sbin/fdisk/fdisk.c Modified: head/sbin/fdisk/fdisk.c ============================================================================== --- head/sbin/fdisk/fdisk.c Sat Jan 22 01:48:12 2011 (r217713) +++ head/sbin/fdisk/fdisk.c Sat Jan 22 05:21:20 2011 (r217714) @@ -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 d 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