[Parted-commits] GNU Parted Official Repository: Changes to 'master'

Jim Meyering meyering at alioth.debian.org
Mon Apr 18 16:40:40 UTC 2011


 libparted/arch/linux.c |   51 +++++++++++++++++++++++++++----------------------
 1 file changed, 29 insertions(+), 22 deletions(-)

New commits:
commit 6c118916e1916449df99613f38ee0915490d29d8
Author: Jim Meyering <meyering at redhat.com>
Date:   Sun Apr 17 14:25:41 2011 +0200

    linux: don't free invalid pointer upon asprintf failure
    
    * libparted/arch/linux.c (_device_get_part_path): When asprintf
    fails, it leaves its first argument in an undefined state, and
    hence that pointer must not be freed.  However, here, in two
    places we could potentially free an invalid pointer.  Use
    zasprintf; then the pointer is either NULL or allocated,
    and hence always freeable.

diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index 5c73a55..fa51b73 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -2740,15 +2740,15 @@ _dm_add_partition (PedDisk* disk, PedPartition* part)
 
         dev_name = dm_task_get_name (task);
 
-        if (asprintf (&vol_name, "%sp%d", dev_name, part->num) == -1)
+        if ( ! (vol_name = zasprintf ("%sp%d", dev_name, part->num)))
                 goto err;
 
         /* Caution: dm_task_destroy frees dev_name.  */
         dm_task_destroy (task);
         task = NULL;
 
-        if (asprintf (&params, "%d:%d %lld", arch_specific->major,
-                      arch_specific->minor, part->geom.start) == -1)
+        if ( ! (params = zasprintf ("%d:%d %lld", arch_specific->major,
+                                    arch_specific->minor, part->geom.start)))
                 goto err;
 
         task = dm_task_create (DM_DEVICE_CREATE);

commit 5e25f84b6b1e7d84784f597fbf728e2f09034387
Author: Jim Meyering <meyering at redhat.com>
Date:   Thu Apr 14 13:22:28 2011 +0200

    linux: clean up device naming code (no semantic change)
    
    * libparted/arch/linux.c (zasprintf): New function.
    (_device_get_part_path): Clean up:
    Use size_t, not "int" for strlen-returned value.
    Combine mostly duplicate snprintf uses.
    Use zasprintf instead of malloc+snprintf.

diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index c65df3d..5c73a55 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -2195,32 +2195,39 @@ linux_probe_all ()
                 _probe_proc_partitions ();
 }
 
-static char*
-_device_get_part_path (PedDevice* dev, int num)
+static char * _GL_ATTRIBUTE_FORMAT ((__printf__, 1, 2))
+zasprintf (const char *format, ...)
 {
-        int             path_len = strlen (dev->path);
-        int             result_len = path_len + 16;
-        char*           result;
+  va_list args;
+  char *resultp;
+  va_start (args, format);
+  int r = vasprintf (&resultp, format, args);
+  va_end (args);
+  return r < 0 ? NULL : resultp;
+}
 
-        result = (char*) ped_malloc (result_len);
-        if (!result)
-                return NULL;
+static char*
+_device_get_part_path (PedDevice *dev, int num)
+{
+        size_t path_len = strlen (dev->path);
 
+        char *result;
         /* Check for devfs-style /disc => /partN transformation
            unconditionally; the system might be using udev with devfs rules,
            and if not the test is harmless. */
         if (5 < path_len && !strcmp (dev->path + path_len - 5, "/disc")) {
                 /* replace /disc with /path%d */
-                strcpy (result, dev->path);
-                snprintf (result + path_len - 5, 16, "/part%d", num);
-        } else if (dev->type == PED_DEVICE_DAC960
-                        || dev->type == PED_DEVICE_CPQARRAY
-                        || dev->type == PED_DEVICE_ATARAID
-                        || dev->type == PED_DEVICE_DM
-                        || isdigit (dev->path[path_len - 1]))
-                snprintf (result, result_len, "%sp%d", dev->path, num);
-        else
-                snprintf (result, result_len, "%s%d", dev->path, num);
+                result = zasprintf ("%.*s/part%d",
+                                    (int) (path_len - 5), dev->path, num);
+        } else {
+                char const *p = (dev->type == PED_DEVICE_DAC960
+                                 || dev->type == PED_DEVICE_CPQARRAY
+                                 || dev->type == PED_DEVICE_ATARAID
+                                 || dev->type == PED_DEVICE_DM
+                                 || isdigit (dev->path[path_len - 1])
+                                 ? "p" : "");
+                result = zasprintf ("%s%s%d", dev->path, p, num);
+        }
 
         return result;
 }

commit f6e435678b5c0c4fe92a4a8aa1f1900d5b5acd05
Author: Jim Meyering <meyering at redhat.com>
Date:   Sun Apr 17 14:15:54 2011 +0200

    don't reference before start of buffer for short device name
    
    * libparted/arch/linux.c (_device_get_part_path): Avoid invalid
    reference to memory before dev->path when its length is 4 or less.

diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index e77210e..c65df3d 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -2209,7 +2209,7 @@ _device_get_part_path (PedDevice* dev, int num)
         /* Check for devfs-style /disc => /partN transformation
            unconditionally; the system might be using udev with devfs rules,
            and if not the test is harmless. */
-        if (!strcmp (dev->path + path_len - 5, "/disc")) {
+        if (5 < path_len && !strcmp (dev->path + path_len - 5, "/disc")) {
                 /* replace /disc with /path%d */
                 strcpy (result, dev->path);
                 snprintf (result + path_len - 5, 16, "/part%d", num);



More information about the Parted-commits mailing list