[parted-devel] [PATCH] Handle ped_disk_get_last_partition_num(disk) == -1 gracefully

Soren Hansen soren at canonical.com
Wed Aug 6 17:17:50 UTC 2008


If ped_disk_get_last_partition_num(disk) fails (which it will if disk is
a loopback device), it will return -1. This in turn will cause
_disk_sync_part_table and  _dm_reread_part_table to attempt to malloc a
sizeof(int)*-1, which instantly causes a "Fatal: Out of memory".

This patch detects this case, and pretends the last partition id is 0.

Signed-off-by: Soren Hansen <soren at canonical.com>

---
 libparted/arch/linux.c |   25 +++++++++++++++++++++----
 1 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index cf26322..04f32e2 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -2212,11 +2212,21 @@ static int
 _disk_sync_part_table (PedDisk* disk)
 {
         int     i;
-        int     last = PED_MIN (ped_disk_get_last_partition_num (disk), 16);
-        int*    rets = ped_malloc(sizeof(int) * last);
-        int*    errnums = ped_malloc(sizeof(int) * last);
+        int     last;
+        int*    rets;
+        int*    errnums;
         int     ret = 1;
 
+        last = ped_disk_get_last_partition_num (disk);
+
+        if (last < 0)
+            last = 0;
+
+        last = PED_MIN(last, 16);
+
+        rets = ped_malloc(sizeof(int) * last);
+        errnums = ped_malloc(sizeof(int) * last);
+
         for (i = 1; i <= last; i++) {
                 rets[i - 1] = _blkpg_remove_partition (disk, i);
                 errnums[i - 1] = errno;
@@ -2434,9 +2444,16 @@ static int
 _dm_reread_part_table (PedDisk* disk)
 {
         int     rc = 1;
-        int     last = PED_MIN (ped_disk_get_last_partition_num (disk), 16);
+        int     last;
         int     i;
 
+        last = ped_disk_get_last_partition_num (disk);
+
+        if (last < 0)
+            last = 0;
+
+        last = PED_MIN (last, 16);
+
         sync();
         if (!_dm_remove_parts(disk->dev))
                 rc = 0;
-- 
1.5.4.3

-- 
Soren Hansen               | 
Virtualisation specialist  | Ubuntu Server Team
Canonical Ltd.             | http://www.ubuntu.com/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 315 bytes
Desc: Digital signature
Url : http://lists.alioth.debian.org/pipermail/parted-devel/attachments/20080806/925bc423/attachment.pgp 


More information about the parted-devel mailing list