cdown / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

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

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