]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: fix alignment logic for tiny partitions
authorKarel Zak <kzak@redhat.com>
Wed, 29 Apr 2020 09:10:54 +0000 (11:10 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 29 Apr 2020 09:10:54 +0000 (11:10 +0200)
Addresses: https://github.com/karelzak/util-linux/issues/1018 (second case)
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/alignment.c
libfdisk/src/dos.c

index 4ae5ff08f93603ff840cd061b3341871627aa371..cf7918056336145acfd548f4a2a079716bd68699 100644 (file)
@@ -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;
index 79a55528dfd6967301a5980450e33d8b5adca115..6268d2f3afec68c6bc7cfa79343f92ff6f37511f 100644 (file)
@@ -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);