From 33f7bb2f9967856afac2411831ef16dcf95746ab Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Tue, 10 Aug 2021 15:49:48 -0700 Subject: parted: Escape colons and backslashes in machine output The device path, device model, and partition name could all contain colons or backslashes. This escapes all of these with a backslash. Thanks to Arvin Schnell for the patch. --- parted/parted.c | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/parted/parted.c b/parted/parted.c index 22b5818..65b5ab2 100644 --- a/parted/parted.c +++ b/parted/parted.c @@ -985,6 +985,32 @@ _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 (2 * strlen(str) + 1); + if (!dest) + return NULL; + + for (i = 0, j = 0; str[i] != '\0'; i++, j++) { + switch (str[i]) { + case ':': + case '\\': + dest[j++] = '\\'; + /* fallthrough */ + default: + dest[j] = str[i]; + break; + } + } + dest[j] = '\0'; + + return dest; +} + static void _print_disk_info (const PedDevice *dev, const PedDisk *diskp) { @@ -1005,6 +1031,9 @@ _print_disk_info (const PedDevice *dev, const PedDisk *diskp) char *disk_flags = disk_print_flags (diskp); if (opt_machine_mode) { + char *escaped_path = _escape_machine_string (dev->path); + char *escaped_model = _escape_machine_string (dev->model); + switch (default_unit) { case PED_UNIT_CHS: puts ("CHS;"); break; @@ -1015,9 +1044,11 @@ _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], + escaped_path, end, transport[dev->type], dev->sector_size, dev->phys_sector_size, - pt_name, dev->model, disk_flags); + pt_name, escaped_model, disk_flags); + free (escaped_path); + free (escaped_model); } else { printf (_("Model: %s (%s)\n"), dev->model, transport[dev->type]); @@ -1289,8 +1320,11 @@ do_print (PedDevice** dev, PedDisk** diskp) putchar (':'); if (has_name) - printf ("%s:", ped_partition_get_name (part)); - else + { + char *escaped_name = _escape_machine_string (ped_partition_get_name (part)); + printf ("%s:", escaped_name); + free (escaped_name); + } else putchar (':'); char *flags = partition_print_flags (part); -- cgit v1.2.1