The problem is how fdisk_partition_cmp_start() compare numbers, the
function returns result from "a->start - b->start", unfortunately the
numbers are uint64, but function returns "int".
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=
1170191
Signed-off-by: Karel Zak <kzak@redhat.com>
_max1 > _max2 ? _max1 : _max2; })
#endif
+#ifndef cmp_numbers
+# define cmp_numbers(x, y) __extension__ ({ \
+ __typeof__(x) _a = (x); \
+ __typeof__(y) _b = (y); \
+ (void) (&_a == &_b); \
+ a == b ? 0 : a > b ? 1 : -1; })
+#endif
+
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
if (!blkdev_get_sectors(cxt->dev_fd, &nsects))
cxt->total_sectors = (nsects / (cxt->sector_size >> 9));
+ DBG(CXT, ul_debugobj(cxt, "total sectors: %ju (ioctl=%ju)",
+ (uintmax_t) cxt->total_sectors,
+ (uintmax_t) nsects));
+
/* what the kernel/bios thinks the geometry is */
blkdev_get_geometry(cxt->dev_fd, &cxt->geom.heads, (unsigned int *) &cxt->geom.sectors);
if (be->offset == 0)
return -1;
- return ae->offset - be->offset;
+ return cmp_numbers(ae->offset, be->offset);
}
/*
if (bu)
return -1;
- return gpt_partition_start(ae) - gpt_partition_start(be);
+ return cmp_numbers(gpt_partition_start(ae), gpt_partition_start(be));
}
/* sort partition by start sector */
int fdisk_partition_cmp_start(struct fdisk_partition *a,
struct fdisk_partition *b)
{
- return a->start - b->start;
+ int is_a = FDISK_IS_UNDEF(a->start),
+ is_b = FDISK_IS_UNDEF(b->start);
+
+ if (!is_a && !is_b)
+ return 0;
+ if (!is_a)
+ return -1;
+ if (!is_b)
+ return 1;
+
+ return cmp_numbers(a->start, b->start);
}
/**
rc = fdisk_get_partitions(cxt, &parts);
if (rc)
goto done;
+
fdisk_table_sort_partitions(parts, fdisk_partition_cmp_start);
fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
last = cxt->first_lba;
grain = cxt->grain > cxt->sector_size ? cxt->grain / cxt->sector_size : 1;
+ DBG(CXT, ul_debugobj(cxt, "initialized: last=%ju, grain=%ju", last, grain));
+
/* analyze gaps between partitions */
while (rc == 0 && fdisk_table_next_partition(parts, &itr, &pa) == 0) {
+
+ DBG(CXT, ul_debugobj(cxt, "partno=%zu, start=%ju", pa->partno, pa->start));
+
if (!pa->used || pa->wholedisk || fdisk_partition_is_nested(pa)
|| !fdisk_partition_has_start(pa))
continue;