dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

Blame SOURCES/0075-libfdisk-gpt-be-more-careful-with-64bit-constants.patch

05ad79
From 428be59e33d0875cdf5bf602a75328fb3d7c58ad Mon Sep 17 00:00:00 2001
05ad79
From: Karel Zak <kzak@redhat.com>
05ad79
Date: Tue, 21 Jun 2016 14:06:14 +0200
05ad79
Subject: [PATCH 75/84] libfdisk: (gpt) be more careful with 64bit constants
05ad79
05ad79
It's probably more robust (and readable) to be explicit when we count
05ad79
with constant and 64bit numbers.
05ad79
05ad79
Upstream: http://github.com/karelzak/util-linux/commit/0a7cdf80606cc0670ef7f740d37640b05932e0ce
05ad79
Addresses: http://bugzilla.redhat.com/show_bug.cgi?id=1344482
05ad79
Signed-off-by: Karel Zak <kzak@redhat.com>
05ad79
---
05ad79
 libfdisk/src/gpt.c | 40 ++++++++++++++++++++--------------------
05ad79
 1 file changed, 20 insertions(+), 20 deletions(-)
05ad79
05ad79
diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c
05ad79
index 482d453..d3bdc2d 100644
05ad79
--- a/libfdisk/src/gpt.c
05ad79
+++ b/libfdisk/src/gpt.c
05ad79
@@ -53,7 +53,7 @@
05ad79
 #define GPT_MBR_PROTECTIVE  1
05ad79
 #define GPT_MBR_HYBRID      2
05ad79
 
05ad79
-#define GPT_PRIMARY_PARTITION_TABLE_LBA 0x00000001
05ad79
+#define GPT_PRIMARY_PARTITION_TABLE_LBA 0x00000001ULL
05ad79
 
05ad79
 #define EFI_PMBR_OSTYPE     0xEE
05ad79
 #define MSDOS_MBR_SIGNATURE 0xAA55
05ad79
@@ -364,7 +364,7 @@ static int gpt_mknew_pmbr(struct fdisk_context *cxt)
05ad79
 	pmbr->partition_record[0].end_track    = 0xFF;
05ad79
 	pmbr->partition_record[0].starting_lba = cpu_to_le32(1);
05ad79
 	pmbr->partition_record[0].size_in_lba  =
05ad79
-		cpu_to_le32(min((uint32_t) cxt->total_sectors - 1, 0xFFFFFFFF));
05ad79
+		cpu_to_le32((uint32_t) min(cxt->total_sectors - 1ULL, 0xFFFFFFFFULL));
05ad79
 
05ad79
 	return 0;
05ad79
 }
05ad79
@@ -379,14 +379,14 @@ static void gpt_mknew_header_common(struct fdisk_context *cxt,
05ad79
 	header->my_lba = cpu_to_le64(lba);
05ad79
 
05ad79
 	if (lba == GPT_PRIMARY_PARTITION_TABLE_LBA) { /* primary */
05ad79
-		header->alternative_lba = cpu_to_le64(cxt->total_sectors - 1);
05ad79
-		header->partition_entry_lba = cpu_to_le64(2);
05ad79
+		header->alternative_lba = cpu_to_le64(cxt->total_sectors - 1ULL);
05ad79
+		header->partition_entry_lba = cpu_to_le64(2ULL);
05ad79
 	} else { /* backup */
05ad79
 		uint64_t esz = le32_to_cpu(header->npartition_entries) * sizeof(struct gpt_entry);
05ad79
 		uint64_t esects = (esz + cxt->sector_size - 1) / cxt->sector_size;
05ad79
 
05ad79
 		header->alternative_lba = cpu_to_le64(GPT_PRIMARY_PARTITION_TABLE_LBA);
05ad79
-		header->partition_entry_lba = cpu_to_le64(cxt->total_sectors - 1 - esects);
05ad79
+		header->partition_entry_lba = cpu_to_le64(cxt->total_sectors - 1ULL - esects);
05ad79
 	}
05ad79
 }
05ad79
 
05ad79
@@ -451,8 +451,8 @@ static int gpt_mknew_header(struct fdisk_context *cxt,
05ad79
 	header->npartition_entries     = cpu_to_le32(GPT_NPARTITIONS);
05ad79
 	header->sizeof_partition_entry = cpu_to_le32(sizeof(struct gpt_entry));
05ad79
 
05ad79
-	last = cxt->total_sectors - 2 - esz;
05ad79
-	first = esz + 2;
05ad79
+	last = cxt->total_sectors - 2ULL - esz;
05ad79
+	first = esz + 2ULL;
05ad79
 
05ad79
 	if (first < cxt->first_lba && cxt->first_lba < last)
05ad79
 		/* Align according to topology */
05ad79
@@ -520,7 +520,7 @@ check_hybrid:
05ad79
 	 */
05ad79
 	if (ret == GPT_MBR_PROTECTIVE) {
05ad79
 		if (le32_to_cpu(pmbr->partition_record[0].size_in_lba) !=
05ad79
-		    min((uint32_t) cxt->total_sectors - 1, 0xFFFFFFFF))
05ad79
+		    (uint32_t) min(cxt->total_sectors - 1ULL, 0xFFFFFFFFULL))
05ad79
 			ret = 0;
05ad79
 	}
05ad79
 done:
05ad79
@@ -538,7 +538,7 @@ static uint64_t last_lba(struct fdisk_context *cxt)
05ad79
 	}
05ad79
 
05ad79
 	if (S_ISBLK(s.st_mode))
05ad79
-		return cxt->total_sectors - 1;
05ad79
+		return cxt->total_sectors - 1ULL;
05ad79
 	else if (S_ISREG(s.st_mode)) {
05ad79
 		uint64_t sectors = s.st_size >> cxt->sector_size;
05ad79
 		return (sectors / cxt->sector_size) - 1ULL;
05ad79
@@ -554,7 +554,7 @@ static ssize_t read_lba(struct fdisk_context *cxt, uint64_t lba,
05ad79
 
05ad79
 	if (lseek(cxt->dev_fd, offset, SEEK_SET) == (off_t) -1)
05ad79
 		return -1;
05ad79
-	return read(cxt->dev_fd, buffer, bytes) != bytes;
05ad79
+	return read(cxt->dev_fd, buffer, bytes) != (ssize_t) bytes;
05ad79
 }
05ad79
 
05ad79
 
05ad79
@@ -908,7 +908,7 @@ static uint64_t find_first_available(struct gpt_header *header,
05ad79
 			if (first < gpt_partition_start(&e[i]))
05ad79
 				continue;
05ad79
 			if (first <= gpt_partition_end(&e[i])) {
05ad79
-				first = gpt_partition_end(&e[i]) + 1;
05ad79
+				first = gpt_partition_end(&e[i]) + 1ULL;
05ad79
 				first_moved = 1;
05ad79
 			}
05ad79
 		}
05ad79
@@ -937,7 +937,7 @@ static uint64_t find_last_free(struct gpt_header *header,
05ad79
 		uint64_t ps = gpt_partition_start(&e[i]);
05ad79
 
05ad79
 		if (nearest_start > ps && ps > start)
05ad79
-			nearest_start = ps - 1;
05ad79
+			nearest_start = ps - 1ULL;
05ad79
 	}
05ad79
 
05ad79
 	return nearest_start;
05ad79
@@ -960,7 +960,7 @@ static uint64_t find_last_free_sector(struct gpt_header *header,
05ad79
 		for (i = 0; i < le32_to_cpu(header->npartition_entries); i++) {
05ad79
 			if ((last >= gpt_partition_start(&e[i])) &&
05ad79
 			    (last <= gpt_partition_end(&e[i]))) {
05ad79
-				last = gpt_partition_start(&e[i]) - 1;
05ad79
+				last = gpt_partition_start(&e[i]) - 1ULL;
05ad79
 				last_moved = 1;
05ad79
 			}
05ad79
 		}
05ad79
@@ -986,7 +986,7 @@ static uint64_t find_first_in_largest(struct gpt_header *header, struct gpt_entr
05ad79
 		first_sect =  find_first_available(header, e, start);
05ad79
 		if (first_sect != 0) {
05ad79
 			last_sect = find_last_free(header, e, first_sect);
05ad79
-			segment_size = last_sect - first_sect + 1;
05ad79
+			segment_size = last_sect - first_sect + 1ULL;
05ad79
 
05ad79
 			if (segment_size > selected_size) {
05ad79
 				selected_size = segment_size;
05ad79
@@ -1026,7 +1026,7 @@ static uint64_t get_free_sectors(struct fdisk_context *cxt, struct gpt_header *h
05ad79
 				largest_seg = segment_sz;
05ad79
 			totfound += segment_sz;
05ad79
 			num++;
05ad79
-			start = last_sect + 1;
05ad79
+			start = last_sect + 1ULL;
05ad79
 		}
05ad79
 	} while (first_sect);
05ad79
 
05ad79
@@ -1165,7 +1165,7 @@ void gpt_list_table(struct fdisk_context *cxt,
05ad79
 			continue;
05ad79
 
05ad79
 		/* the partition has to inside usable range */
05ad79
-		if (start < fu || start + size - 1 > lu)
05ad79
+		if (start < fu || start + size - 1ULL > lu)
05ad79
 			continue;
05ad79
 
05ad79
 		name = encode_to_utf8((unsigned char *)gpt->ents[i].partition_name,
05ad79
@@ -1266,11 +1266,11 @@ static int gpt_write_pmbr(struct fdisk_context *cxt)
05ad79
 	 * Set size_in_lba to the size of the disk minus one. If the size of the disk
05ad79
 	 * is too large to be represented by a 32bit LBA (2Tb), set it to 0xFFFFFFFF.
05ad79
 	 */
05ad79
-	if (cxt->total_sectors - 1 > 0xFFFFFFFFULL)
05ad79
+	if (cxt->total_sectors - 1ULL > 0xFFFFFFFFULL)
05ad79
 		pmbr->partition_record[0].size_in_lba = cpu_to_le32(0xFFFFFFFF);
05ad79
 	else
05ad79
 		pmbr->partition_record[0].size_in_lba =
05ad79
-			cpu_to_le32(cxt->total_sectors - 1UL);
05ad79
+			cpu_to_le32((uint32_t) (cxt->total_sectors - 1ULL));
05ad79
 
05ad79
 	offset = GPT_PMBR_LBA * cxt->sector_size;
05ad79
 	if (offset != lseek(cxt->dev_fd, offset, SEEK_SET))
05ad79
@@ -1308,7 +1308,7 @@ static int gpt_write_disklabel(struct fdisk_context *cxt)
05ad79
 		goto err0;
05ad79
 
05ad79
 	/* check that the backup header is properly placed */
05ad79
-	if (le64_to_cpu(gpt->pheader->alternative_lba) < cxt->total_sectors - 1)
05ad79
+	if (le64_to_cpu(gpt->pheader->alternative_lba) < cxt->total_sectors - 1ULL)
05ad79
 		/* TODO: correct this (with user authorization) and write */
05ad79
 		goto err0;
05ad79
 
05ad79
@@ -1645,7 +1645,7 @@ static int gpt_add_partition(
05ad79
 
05ad79
 		user_l = fdisk_ask_number_get_result(ask);
05ad79
 		if (fdisk_ask_number_is_relative(ask))
05ad79
-			user_l = fdisk_align_lba_in_range(cxt, user_l, user_f, dflt_l) - 1;
05ad79
+			user_l = fdisk_align_lba_in_range(cxt, user_l, user_f, dflt_l) - 1ULL;
05ad79
 		if (user_l > user_f && user_l <= disk_l)
05ad79
 			break;
05ad79
 	}
05ad79
-- 
05ad79
2.7.4
05ad79