[parted-devel] [PATCH] parted: Escape printed device path in machine mode

Sebastian Parschauer sparschauer at suse.de
Tue Nov 7 20:01:52 UTC 2017


The function _print_disk_info() uses the colon ':' as the separator
but the device path it prints can contain that character as well.
In that case parsing fails.
So introduce the function _escape_machine_string() to allocate an
output string twice as big as the input string and escape ':' and
'\' with a '\'. Print the escaped path and free it again. Ignore
the NULL pointer dereference in out-of-memory situation like it
is done for the other allocations in _print_disk_info() as well.

Resolves: bsc#1066467

Reported-by: Arvin Schnell <aschnell at suse.com>
Signed-off-by: Sebastian Parschauer <sparschauer at suse.de>
---
 parted/parted.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/parted/parted.c b/parted/parted.c
index c471d4976d06..d289b25fe6cb 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -980,6 +980,30 @@ _print_disk_geometry (const PedDevice *dev)
         free (cyl_size);
 }
 
+static char *
+_escape_machine_string (const char *str)
+{
+        size_t i, j;
+        char *dest;
+
+        dest = ped_malloc ((strlen(str) + 1) * 2);
+        if (!dest)
+                return NULL;
+
+        for (i = 0, j = 0; str[i] != '\0'; i++, j++) {
+             switch (str[i]) {
+                 case ':':
+                 case '\\':
+                     dest[j++] = '\\';
+                 default:
+                     dest[j] = str[i];
+                     break;
+             }
+        }
+        dest[j] = '\0';
+        return dest;
+}
+
 static void
 _print_disk_info (const PedDevice *dev, const PedDisk *diskp)
 {
@@ -999,6 +1023,8 @@ _print_disk_info (const PedDevice *dev, const PedDisk *diskp)
         char *disk_flags = disk_print_flags (diskp);
 
         if (opt_machine_mode) {
+            char *path = _escape_machine_string (dev->path);
+
             switch (default_unit) {
                 case PED_UNIT_CHS:      puts ("CHS;");
                                         break;
@@ -1009,9 +1035,10 @@ _print_disk_info (const PedDevice *dev, const PedDisk *diskp)
 
             }
             printf ("%s:%s:%s:%lld:%lld:%s:%s:%s;\n",
-                    dev->path, end, transport[dev->type],
+                    path, end, transport[dev->type],
                     dev->sector_size, dev->phys_sector_size,
                     pt_name, dev->model, disk_flags);
+            free(path);
         } else {
             printf (_("Model: %s (%s)\n"),
                     dev->model, transport[dev->type]);
-- 
2.13.6




More information about the parted-devel mailing list