From b362a4cb986f7eea4b884c800719971502e2e8a8 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 25 Apr 2016 11:59:06 +0200 Subject: libfdisk: don't offer zero length freespace Reported-by: Kay Sievers Signed-off-by: Karel Zak --- libfdisk/src/table.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'libfdisk') diff --git a/libfdisk/src/table.c b/libfdisk/src/table.c index 9be57139e..6d8cbc25a 100644 --- a/libfdisk/src/table.c +++ b/libfdisk/src/table.c @@ -394,6 +394,8 @@ static int new_freespace(struct fdisk_context *cxt, struct fdisk_partition *parent, struct fdisk_partition **pa) { + fdisk_sector_t aligned_start, size; + assert(cxt); assert(pa); @@ -401,17 +403,26 @@ static int new_freespace(struct fdisk_context *cxt, if (start == end) return 0; - *pa = fdisk_new_partition(); - if (!*pa) - return -ENOMEM; assert(start); assert(end); assert(end > start); + aligned_start = fdisk_align_lba_in_range(cxt, start, start, end); + size = end - aligned_start + 1ULL; + + if (size == 0) { + DBG(TAB, ul_debug("ignore freespace (aligned size is zero)")); + return 0; + } + + *pa = fdisk_new_partition(); + if (!*pa) + return -ENOMEM; + (*pa)->freespace = 1; - (*pa)->start = fdisk_align_lba_in_range(cxt, start, start, end); - (*pa)->size = end - (*pa)->start + 1ULL; + (*pa)->start = aligned_start; + (*pa)->size = size; if (parent) (*pa)->parent_partno = parent->partno; -- cgit v1.2.3-55-g7522