]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: fix cfdisk freespace analyze
authorKarel Zak <kzak@redhat.com>
Thu, 4 Dec 2014 09:27:39 +0000 (10:27 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 4 Dec 2014 09:27:39 +0000 (10:27 +0100)
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>
include/c.h
libfdisk/src/alignment.c
libfdisk/src/dos.c
libfdisk/src/gpt.c
libfdisk/src/partition.c
libfdisk/src/table.c

index 0f6e5b29becdb4e615e2b0d4960361314e962818..1425839563105c5b678b41d875aa4c6df6b4356d 100644 (file)
        _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
index 93cb80b32153802852f8d6b3116b79a1fd3d7c05..b1f3ee5c3d94db9c6d28a192eb0eb2a7fd6d89ef 100644 (file)
@@ -416,6 +416,10 @@ int fdisk_discover_geometry(struct fdisk_context *cxt)
        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);
 
index abafcc24835e956dc756b2457c1287e0608dfe36..16391806e8dd42b0f2e859cb158836eb9944cfde 100644 (file)
@@ -1985,7 +1985,7 @@ static int cmp_ebr_offsets(const void *a, const void *b)
        if (be->offset == 0)
                return -1;
 
-       return ae->offset - be->offset;
+       return cmp_numbers(ae->offset, be->offset);
 }
 
 /*
index 6448a054a2b6c0b04cee46de10a50adeffa67601..fc18441d627c19f418fd30802a3c6534321304b2 100644 (file)
@@ -2420,7 +2420,7 @@ static int gpt_entry_cmp_start(const void *a, const void *b)
        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 */
index 7d4d57669fed03383953b4bb46cd42fef829be14..90276416e09a6a15a72e8a175d5a34540e1bd449 100644 (file)
@@ -180,7 +180,17 @@ int fdisk_partition_has_start(struct fdisk_partition *pa)
 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);
 }
 
 /**
index 8adbd7f3661aad70dafcd32337ff4a912ebcb5f0..9747f7dc8313e58ea9addc5059b3dde0dd890d7a 100644 (file)
@@ -497,13 +497,19 @@ int fdisk_get_freespaces(struct fdisk_context *cxt, struct fdisk_table **tb)
        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;