File parted-escape-printed-device-path.patch of Package parted.15765

From: Sebastian Parschauer <sparschauer@suse.de>
Date: Tue, 7 Nov 2017 20:14:06 +0100
Subject: parted: Escape printed device path in machine mode
References: bsc#1066467
Patch-mainline: submitted, 2017-11-07

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@suse.com>
Signed-off-by: Sebastian Parschauer <sparschauer@suse.de>
---
 parted/parted.c |   29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

--- a/parted/parted.c
+++ b/parted/parted.c
@@ -1038,6 +1038,30 @@ _print_disk_geometry (const PedDevice *d
         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)
 {
@@ -1058,6 +1082,8 @@ _print_disk_info (const PedDevice *dev,
         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;
@@ -1068,9 +1094,10 @@ _print_disk_info (const PedDevice *dev,
 
             }
             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]);
openSUSE Build Service is sponsored by