OSDN Git Service

gpt: don't malfunction on big-endian systems
authorJim Meyering <meyering@redhat.com>
Tue, 6 Oct 2009 18:10:00 +0000 (20:10 +0200)
committerJim Meyering <meyering@redhat.com>
Tue, 6 Oct 2009 18:28:54 +0000 (20:28 +0200)
Numerous GPT tests would fail when run on e.g., big-endian PPC.
* libparted/labels/gpt.c (gpt_read): Now that we use the
SizeOfPartitionEntry member, be sure to convert from GPT's
little-endian on-disk format to to CPU endianness.
This bug was introduced via commit 14cce9b2, 2009-06-10, "gpt:
fix gpt_read to read all of the partition entries correctly".
* libparted/labels/gpt.c (_header_is_valid): Also convert it here.
Add a test to ensure that the partition entry size is no larger
than the slightly arbitrary UINT32_MAX/16.
* NEWS (Bug fixes): Mention it.

NEWS
libparted/labels/gpt.c

diff --git a/NEWS b/NEWS
index 04741a2..d4147ad 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -18,6 +18,9 @@ GNU parted NEWS                                    -*- outline -*-
 
 ** Bug fixes
 
+  big-endian systems can once again read GPT partition tables
+  [bug introduced in parted-1.9.0]
+
   ped_partition_is_busy no longer calls libparted's exception handler,
   since doing so caused trouble with anaconda/pyparted when operating on
   dmraid devices.
index cc9bcdc..b4549ef 100644 (file)
@@ -608,8 +608,10 @@ _header_is_valid (const PedDevice* dev, GuidPartitionTableHeader_t* gpt)
         * the SizeOfPartitionEntry must be a multiple of 8 and
         * no smaller than the size of the PartitionEntry structure.
         */
-       uint32_t sope = gpt->SizeOfPartitionEntry;
-       if (sope % 8 != 0 || sope < sizeof(GuidPartitionEntry_t) )
+       uint32_t sope = PED_LE32_TO_CPU (gpt->SizeOfPartitionEntry);
+       if (sope % 8 != 0
+            || sope < sizeof (GuidPartitionEntry_t)
+            || (UINT32_MAX >> 4) < sope)
                return 0;
 
        origcrc = gpt->HeaderCRC32;
@@ -911,7 +913,8 @@ gpt_read (PedDisk * disk)
        if (!_parse_header (disk, gpt, &write_back))
                goto error_free_gpt;
 
-       ptes_sectors = ped_div_round_up (gpt->SizeOfPartitionEntry
+       uint32_t p_ent_size = PED_LE32_TO_CPU (gpt->SizeOfPartitionEntry);
+       ptes_sectors = ped_div_round_up (p_ent_size
                                         * gpt_disk_data->entry_count,
                                         disk->dev->sector_size);
 
@@ -926,8 +929,7 @@ gpt_read (PedDisk * disk)
 
        for (i = 0; i < gpt_disk_data->entry_count; i++) {
                GuidPartitionEntry_t* pte
-                 = (GuidPartitionEntry_t*) ((char *)ptes + i
-                                            * gpt->SizeOfPartitionEntry);
+                 = (GuidPartitionEntry_t*) ((char *)ptes + i * p_ent_size);
                PedPartition* part;
                PedConstraint* constraint_exact;