bug-parted
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] ui: rewrite disk_print_flags and partition_print_flags


From: Jim Meyering
Subject: [PATCH] ui: rewrite disk_print_flags and partition_print_flags
Date: Fri, 03 Feb 2012 16:13:17 +0100

Here's the patch I mentioned.
As you can see, the existing code had some needless duplication,
not to mention the possible NULL-deref in each function.

>From 9767d0de5074e239d44d44cc6854992bd1ded380 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Fri, 3 Feb 2012 15:55:19 +0100
Subject: [PATCH] ui: rewrite disk_print_flags and partition_print_flags

* parted/parted.c (disk_print_flags): Avoid NULL-dereference on
failed malloc.  Use xrealloc, not ped_realloc.
(partition_print_flags): Likewise; nearly identical code.
---
 parted/parted.c |  100 ++++++++++++++++++++++--------------------------------
 1 files changed, 41 insertions(+), 59 deletions(-)

diff --git a/parted/parted.c b/parted/parted.c
index a1b8c2f..789030a 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -860,38 +860,30 @@ error:
 }

 static char*
-partition_print_flags (PedPartition* part)
+partition_print_flags (PedPartition const *part)
 {
-        PedPartitionFlag        flag;
-        int                     first_flag;
-        const char*             name;
-        char*                   res = ped_malloc(1);
-        void*                   _res = res;
-
-        *res = '\0';
-
-        first_flag = 1;
-        for (flag = ped_partition_flag_next (0); flag;
-             flag = ped_partition_flag_next (flag)) {
-                if (ped_partition_get_flag (part, flag)) {
-                        if (first_flag)
-                                first_flag = 0;
-                        else {
-                                _res = res;
-                                ped_realloc (&_res, strlen (res) + 1 + 2);
-                                res = _res;
-                                strncat (res, ", ", 2);
-                        }
-
-                        name = _(ped_partition_flag_get_name (flag));
-                        _res = res;
-                        ped_realloc (&_res, strlen (res) + 1 + strlen (name));
-                        res = _res;
-                        strcat(res, name);
-                }
+  char *res = xstrdup ("");
+  if (!part)
+    return res;
+
+  PedPartitionFlag flag;
+  size_t res_buf_len = 1;
+  char const *sep = "";
+  for (flag = ped_partition_flag_next (0); flag;
+       flag = ped_partition_flag_next (flag))
+    {
+      if (ped_partition_get_flag (part, flag))
+        {
+          const char *name = _(ped_partition_flag_get_name (flag));
+          size_t new_len = res_buf_len + strlen (sep) + strlen (name);
+          res = xrealloc (res, new_len);
+          stpcpy (stpcpy (res + res_buf_len - 1, sep), name);
+          res_buf_len = new_len;
+          sep = ", ";
         }
+    }

-        return res;
+  return res;
 }

 static int
@@ -903,38 +895,28 @@ partition_print (PedPartition* part)
 static char*
 disk_print_flags (PedDisk const *disk)
 {
-        PedDiskFlag     flag;
-        int             first_flag;
-        const char*     name;
-        char*           res = ped_malloc(1);
-        void*           _res = res;
-
-        *res = '\0';
-        if (!disk)
-            return res;
-
-        first_flag = 1;
-        for (flag = ped_disk_flag_next (0); flag;
-             flag = ped_disk_flag_next (flag)) {
-                if (ped_disk_get_flag (disk, flag)) {
-                        if (first_flag)
-                                first_flag = 0;
-                        else {
-                                _res = res;
-                                ped_realloc (&_res, strlen (res) + 1 + 2);
-                                res = _res;
-                                strncat (res, ", ", 2);
-                        }
-
-                        name = _(ped_disk_flag_get_name (flag));
-                        _res = res;
-                        ped_realloc (&_res, strlen (res) + 1 + strlen (name));
-                        res = _res;
-                        strcat(res, name);
-                }
+  char *res = xstrdup ("");
+  if (!disk)
+    return res;
+
+  PedDiskFlag flag;
+  size_t res_buf_len = 1;
+  char const *sep = "";
+  for (flag = ped_disk_flag_next (0); flag;
+       flag = ped_disk_flag_next (flag))
+    {
+      if (ped_disk_get_flag (disk, flag))
+        {
+          const char *name = _(ped_disk_flag_get_name (flag));
+          size_t new_len = res_buf_len + strlen (sep) + strlen (name);
+          res = xrealloc (res, new_len);
+          stpcpy (stpcpy (res + res_buf_len - 1, sep), name);
+          res_buf_len = new_len;
+          sep = ", ";
         }
+    }

-        return res;
+  return res;
 }

 static void
--
1.7.9.112.gb85f2



reply via email to

[Prev in Thread] Current Thread [Next in Thread]