[PATCH] kpartx: check slice size before looking for partition to remove

Benjamin Marzinski <[email protected]> Tue, 21 Jul 2026 02:27:24 -0400
Newsgroups dev.linux.lists.dm-devel
Message-ID <[email protected]>
After commit 03071031 ("kpartx: fix crash and truncated device creation
with long -p delimiter"), when attempting to remove deleted partitions
on kpartx -u, kpartx started calling dm_find_part() to check if the
partition existed before checking if the slice size was 0 (and thus
should get removed). The problem is that if dm_find_part() returns
DFP_DEVICE_RELOAD because the partition device exists, part_uuid gets
allocated, and if the slices[j].size is non-zero and the partition
device shouldn't be removed, part_uuid wasn't getting freed. Fix this by
reverting to checking the slice size first.

Fixes: 03071031 ("kpartx: fix crash and truncated device creation
with long -p delimiter")

Signed-off-by: Benjamin Marzinski <[email protected]>
---
 kpartx/kpartx.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/kpartx/kpartx.c b/kpartx/kpartx.c
index fc8f9e5e..370d09a1 100644
--- a/kpartx/kpartx.c
+++ b/kpartx/kpartx.c
@@ -577,10 +577,11 @@ main(int argc, char **argv){
 
 			for (j = MAXSLICES-1; j >= 0; j--) {
 				char *part_uuid = NULL, *reason;
-				int res = dm_find_part(mapname, delim, j + 1, uuid,
-						       partname, sizeof(partname),
-						       &part_uuid, verbose);
-				if (slices[j].size || res != DFP_DEVICE_RELOAD)
+
+				if (slices[j].size ||
+				    dm_find_part(mapname, delim, j + 1, uuid, partname,
+						 sizeof(partname), &part_uuid,
+						 verbose) != DFP_DEVICE_RELOAD)
 					continue;
 
 				if (part_uuid && uuid) {
-- 
2.53.0