Subject: bug#17108: [PATCH 1/4] libparted: Check AlternateLBA against LBA-1

From: "Brian C. Lane" <[email-masked]>
Date: Wed, 26 Mar 2014 10:56:19 -0700

commit 9e9588c71e introduced a problem with how the backup gpt
partition's location was checked, and where it was re-written. It was
using LastUsableLBA plus an offset based on the number of partition
entries. This will not always match up with the end of the disk. eg.
during t0210 where the number of partitions is set to 9 instead of 128.

The only way to check the AlternateLBA is against the size of the disk.
If it isn't in LBA - 1 then it is in the wrong place.

The outcome was that t0210 and t0211 were failing becase the location
that it tried to move the backup to was also invalid.

* libparted/labels/gpt.c (gpt_read): Fix check for backup gpt location
---
 libparted/labels/gpt.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
index 42b0360..3e8e603 100644
--- a/libparted/labels/gpt.c
+++ b/libparted/labels/gpt.c
@@ -985,13 +985,7 @@ gpt_read (PedDisk *disk)
     {
       /* Both are valid.  */
 #ifndef DISCOVER_ONLY
-      PedSector gpt_disk_end = PED_LE64_TO_CPU (primary_gpt->LastUsableLBA) + 1;
-      gpt_disk_end += ((PedSector) (PED_LE32_TO_CPU (primary_gpt->NumberOfPartitionEntries)) *
-                       (PedSector) (PED_LE32_TO_CPU (primary_gpt->SizeOfPartitionEntry)) /
-                       disk->dev->sector_size);
-
-      gpt_disk_data->AlternateLBA = PED_LE64_TO_CPU (primary_gpt->AlternateLBA);
-      if (PED_LE64_TO_CPU (primary_gpt->AlternateLBA) != gpt_disk_end)
+	if (PED_LE64_TO_CPU (primary_gpt->AlternateLBA) < disk->dev->length - 1)
         {
           if (ped_exception_throw
                   (PED_EXCEPTION_ERROR,
@@ -1002,7 +996,7 @@ gpt_read (PedDisk *disk)
             {
               ptt_clear_sectors (disk->dev,
                                  PED_LE64_TO_CPU (primary_gpt->AlternateLBA), 1);
-              gpt_disk_data->AlternateLBA = gpt_disk_end;
+              gpt_disk_data->AlternateLBA = disk->dev->length - 1;
               write_back = 1;
             }
         }
-- 
1.8.5.3