From: Karel Zak Date: Wed, 29 Apr 2020 09:10:54 +0000 (+0200) Subject: libfdisk: fix alignment logic for tiny partitions X-Git-Tag: v2.36-rc1~125 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=89c0297bcc313b69347ee110c422e84857bd3265;p=thirdparty%2Futil-linux.git libfdisk: fix alignment logic for tiny partitions Addresses: https://github.com/karelzak/util-linux/issues/1018 (second case) Signed-off-by: Karel Zak --- diff --git a/libfdisk/src/alignment.c b/libfdisk/src/alignment.c index 4ae5ff08f9..cf79180563 100644 --- a/libfdisk/src/alignment.c +++ b/libfdisk/src/alignment.c @@ -140,12 +140,14 @@ fdisk_sector_t fdisk_align_lba_in_range(struct fdisk_context *cxt, { fdisk_sector_t res; - start = fdisk_align_lba(cxt, start, FDISK_ALIGN_UP); - stop = fdisk_align_lba(cxt, stop, FDISK_ALIGN_DOWN); + DBG(CXT, ul_debugobj(cxt, "LBA: align in range <%ju..%ju>", (uintmax_t) start, (uintmax_t) stop)); - if (lba > start && lba < stop - && (lba - start) < (cxt->grain / cxt->sector_size)) { + if (start + (cxt->grain / cxt->sector_size) <= stop) { + start = fdisk_align_lba(cxt, start, FDISK_ALIGN_UP); + stop = fdisk_align_lba(cxt, stop, FDISK_ALIGN_DOWN); + } + if (start + (cxt->grain / cxt->sector_size) > stop) { DBG(CXT, ul_debugobj(cxt, "LBA: area smaller than grain, don't align")); res = lba; goto done; diff --git a/libfdisk/src/dos.c b/libfdisk/src/dos.c index 79a55528df..6268d2f3af 100644 --- a/libfdisk/src/dos.c +++ b/libfdisk/src/dos.c @@ -1236,7 +1236,11 @@ static int add_partition(struct fdisk_context *cxt, size_t n, struct pte *pe = self_pte(cxt, n); assert(pe); + assert(start >= cxt->first_lba); + pe->offset = start - cxt->first_lba; + DBG(LABEL, ul_debug("DOS: setting EBR offset to %ju [start=%ju]", pe->offset, start)); + if (pe->offset == l->ext_offset) { /* must be corrected */ pe->offset++; if (cxt->first_lba == 1) @@ -1337,7 +1341,6 @@ static int add_partition(struct fdisk_context *cxt, size_t n, set_partition(cxt, n, 0, start, stop, sys, fdisk_partition_is_bootable(pa)); if (n > 4) { struct pte *pe = self_pte(cxt, n); - assert(pe); set_partition(cxt, n - 1, 1, pe->offset, stop, MBR_DOS_EXTENDED_PARTITION, 0);