util-linux/libfdisk-fix-alignment-logic-for-tiny-partitions.patch

63 lines
2.2 KiB
Diff

From 89c0297bcc313b69347ee110c422e84857bd3265 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Wed, 29 Apr 2020 11:10:54 +0200
Subject: [PATCH 235/389] libfdisk: fix alignment logic for tiny partitions
Addresses: https://github.com/karelzak/util-linux/issues/1018 (second case)
Signed-off-by: Karel Zak <kzak@redhat.com>
---
libfdisk/src/alignment.c | 10 ++++++----
libfdisk/src/dos.c | 5 ++++-
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/libfdisk/src/alignment.c b/libfdisk/src/alignment.c
index 4ae5ff0..cf79180 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 79a5552..6268d2f 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);
--
1.8.3.1