File util-linux-libfdisk-ebr-missing-gap-1.patch of Package util-linux.43342

From 8b8da020f57414c90981371da71fdf32d2253ac7 Mon Sep 17 00:00:00 2001
From: Martin Jungblut Schreiner <martinjungblut@gmail.com>
Date: Mon, 29 Dec 2025 19:52:56 -0300
Subject: [PATCH 1/5] libfdisk: (dos) fix logical partition start
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

fdisk could accept adjacent logical partitions, causing the EBR for the new
logical partition to be written inside the previous partition’s data area.
This can corrupt the EBR chain.

Fix free-sector search to keep an EBR gap (first_lba) after logical partitions.
---
 libfdisk/src/dos.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/libfdisk/src/dos.c b/libfdisk/src/dos.c
index c88d2a4f2..f85104b28 100644
--- a/libfdisk/src/dos.c
+++ b/libfdisk/src/dos.c
@@ -1201,6 +1201,21 @@ static int find_first_free_sector_in_range(
 				p_start -= cxt->first_lba;
 			if (first < p_start)
 				continue;
+
+			/* if we're placing a logical partition start, ensure
+			there's room for the *next* EBR (stored at start - first_lba).
+			therefore enforce:
+			start(Lnew) >= end(Lprev) + first_lba + 1 */
+			if (logical && first > p_end && (first - p_end) <= cxt->first_lba) {
+				first = p_end + 1 + cxt->first_lba;
+				first_moved = 1;
+
+				if (first > end)
+					return -ENOSPC;
+
+				continue;
+			}
+
 			if (first <= p_end) {
 				first = p_end + 1 + (logical ? cxt->first_lba : 0);
 				first_moved = 1;
-- 
2.51.0

openSUSE Build Service is sponsored by