kpartx: check slice size before looking for partition to remove
Benjamin Marzinski
maintainerPATCH 1/1 kpartx: check slice size before looking for partition to remove
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.
with long -p delimiter")
--- kpartx/kpartx.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
kpartx/kpartx.c +5 -4
@@ -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) {
Signature · 1 line
2.53.0
Quoted 5 lines · Benjamin Marzinski
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).
You're right. My patch changed the order of checks in the DELETE loop, causing dm_find_part() to be called even when slices[j].size is non-zero (meaning the partition is still valid and should not be removed).
Quoted 5 lines
The problem is that if
dm_find_part()returnsDFP_DEVICE_RELOADbecause the partition device exists,part_uuidgets allocated, and if the slices[j].size is non-zero and the partition device shouldn't be removed,part_uuidwasn't getting freed. Fix this by reverting to checking the slice size first.
I see the memory leak now. When slices[j].size != 0 and dm_find_part() returns DFP_DEVICE_RELOAD, we hit the "continue" without freeing part_uuid.
Reverting to checking slice size first is the correct fix - it restores the original optimization of only calling dm_find_part() for partitions that actually need to be removed.
Thank you for catching this!
Quoted 13 lines · Benjamin Marzinski
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 callingdm_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 ifdm_find_part()returnsDFP_DEVICE_RELOADbecause the partition device exists,part_uuidgets allocated, and if the slices[j].size is non-zero and the partition device shouldn't be removed,part_uuidwasn't getting freed. Fix this by
reverting to checking the slice size first.
Quoted 4 lines
Fixes 03071031 ("kpartx: fix crash and truncated device creationwith long -p delimiter")
Signed-off-by Benjamin Marzinski <[email protected]>
Good catch, thanks!