From 77b77e98a133bcc485f8e6d6e52d1b3180962b8e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pali=20Roh=C3=A1r?= Date: Tue, 6 Jul 2021 14:54:40 +0200 Subject: [PATCH] libfdisk: (dos) Add check both begin and end CHS partition parameters MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit There is no reason to check only end CHS partition parameters. Check also begin values and correctly count errors in nerrors variable. fdisk then print correctly summary message "N errors detected.". Signed-off-by: Pali Rohár --- libfdisk/src/dos.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/libfdisk/src/dos.c b/libfdisk/src/dos.c index e65088783a..a4a89f4d78 100644 --- a/libfdisk/src/dos.c +++ b/libfdisk/src/dos.c @@ -1496,38 +1496,50 @@ static int add_logical(struct fdisk_context *cxt, return rc; } -static void check(struct fdisk_context *cxt, size_t n, +static int check(struct fdisk_context *cxt, size_t n, unsigned int h, unsigned int s, unsigned int c, unsigned int lba_sector) { unsigned int chs_sector, real_s, real_c; + int nerrors = 0; if (!is_dos_compatible(cxt)) - return; + return 0; real_s = sector(s) - 1; real_c = cylinder(s, c); chs_sector = (real_c * cxt->geom.heads + h) * cxt->geom.sectors + real_s; - if (!chs_sector) + if (!chs_sector) { fdisk_warnx(cxt, _("Partition %zu: contains sector 0"), n); - if (h >= cxt->geom.heads) + nerrors++; + } + if (h >= cxt->geom.heads) { fdisk_warnx(cxt, _("Partition %zu: head %d greater than " "maximum %d"), n, h + 1, cxt->geom.heads); - if (real_s >= cxt->geom.sectors) + nerrors++; + } + if (real_s >= cxt->geom.sectors) { fdisk_warnx(cxt, _("Partition %zu: sector %d greater than " "maximum %ju"), n, real_s + 1, (uintmax_t) cxt->geom.sectors); - if (real_c >= cxt->geom.cylinders) + nerrors++; + } + if (real_c >= cxt->geom.cylinders) { fdisk_warnx(cxt, _("Partition %zu: cylinder %d greater than " "maximum %ju"), n, real_c + 1, (uintmax_t) cxt->geom.cylinders); - - if (cxt->geom.cylinders <= 1024 && lba_sector != chs_sector) + nerrors++; + } + if (cxt->geom.cylinders <= 1024 && lba_sector != chs_sector) { fdisk_warnx(cxt, _("Partition %zu: LBA sector %u " "disagrees with C/H/S calculated sector %u"), n, lba_sector, chs_sector); + nerrors++; + } + + return nerrors; } /* check_consistency() and long2chs() added Sat Mar 6 12:28:16 1993, @@ -1652,7 +1664,8 @@ static int dos_verify_disklabel(struct fdisk_context *cxt) nerrors++; } - check(cxt, i + 1, p->eh, p->es, p->ec, last[i]); + nerrors += check(cxt, i + 1, p->bh, p->bs, p->bc, first[i]); + nerrors += check(cxt, i + 1, p->eh, p->es, p->ec, last[i]); total += last[i] + 1 - first[i]; if (i == 0) -- 2.47.2