From 298050285086407f07e0c0821c3bba2131792768 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Fri, 23 Dec 2011 19:25:24 +0100 Subject: [PATCH] Fix few bugs and memory leaks --- grub-core/kern/emu/hostdisk.c | 1 + util/getroot.c | 2 +- util/grub-mkconfig_lib.in | 2 +- util/grub-probe.c | 26 ++++++++---- util/ieee1275/ofpath.c | 77 +++++++++++++++++++++++++---------- 5 files changed, 75 insertions(+), 33 deletions(-) diff --git a/grub-core/kern/emu/hostdisk.c b/grub-core/kern/emu/hostdisk.c index d9cf07cf4..b3d35dd43 100644 --- a/grub-core/kern/emu/hostdisk.c +++ b/grub-core/kern/emu/hostdisk.c @@ -1940,6 +1940,7 @@ grub_util_biosdisk_get_grub_dev (const char *os_dev) name = grub_xasprintf ("%s,%s", disk->name, partname); free (partname); + grub_disk_close (disk); return name; } diff --git a/util/getroot.c b/util/getroot.c index a4bc9f0af..967e4b62b 100644 --- a/util/getroot.c +++ b/util/getroot.c @@ -1159,7 +1159,7 @@ grub_util_pull_device (const char *os_dev) return; default: /* GRUB_DEV_ABSTRACTION_NONE */ - grub_util_biosdisk_get_grub_dev (os_dev); + free (grub_util_biosdisk_get_grub_dev (os_dev)); return; } } diff --git a/util/grub-mkconfig_lib.in b/util/grub-mkconfig_lib.in index 13897986b..d7549c540 100644 --- a/util/grub-mkconfig_lib.in +++ b/util/grub-mkconfig_lib.in @@ -149,7 +149,7 @@ prepare_grub_to_access_device () echo "set root='`"${grub_probe}" --device "${device}" --target=compatibility_hint`'" if fs_uuid="`"${grub_probe}" --device "${device}" --target=fs_uuid 2> /dev/null`" ; then hints="`"${grub_probe}" --device "${device}" --target=hints_string 2> /dev/null`" - echo "if [ x$feature_platform_search_hint = xy ]; then" + echo "if [ x\$feature_platform_search_hint = xy ]; then" echo " search --no-floppy --fs-uuid --set=root ${hints} ${fs_uuid}" echo "else" echo " search --no-floppy --fs-uuid --set=root ${fs_uuid}" diff --git a/util/grub-probe.c b/util/grub-probe.c index e33a2ad10..250044637 100644 --- a/util/grub-probe.c +++ b/util/grub-probe.c @@ -67,7 +67,7 @@ enum { PRINT_COMPATIBILITY_HINT }; -int print = PRINT_FS; +static int print = PRINT_FS; static unsigned int argument_is_device = 0; static void @@ -253,7 +253,11 @@ static void print_full_name (const char *drive, grub_device_t dev) { if (dev->disk->partition) - printf ("%s,%s", drive, grub_partition_get_name (dev->disk->partition)); + { + char *pname = grub_partition_get_name (dev->disk->partition); + printf ("%s,%s", drive, pname); + free (pname); + } else printf ("%s", drive); } @@ -347,14 +351,17 @@ probe (const char *path, char *device_name) if (print == PRINT_HINT_STR) { const char *orig_path = grub_util_devname_to_ofpath (device_name); - char *ofpath = escape_of_path (orig_path); char *biosname, *bare, *efi; const char *map; - printf ("--hint-ieee1275="); - print_full_name (ofpath, dev); - printf (" "); - free (ofpath); + if (orig_path) + { + char *ofpath = escape_of_path (orig_path); + printf ("--hint-ieee1275="); + print_full_name (ofpath, dev); + printf (" "); + free (ofpath); + } biosname = guess_bios_drive (device_name); if (biosname) @@ -381,7 +388,7 @@ probe (const char *path, char *device_name) print_full_name (bare, dev); printf (" "); } - free (biosname); + free (bare); /* FIXME: Add ARC hint. */ @@ -404,7 +411,8 @@ probe (const char *path, char *device_name) map = grub_util_biosdisk_get_compatibility_hint (dev->disk); if (map) { - printf ("%s\n", map); + print_full_name (map, dev); + printf ("\n"); goto end; } biosname = guess_bios_drive (device_name); diff --git a/util/ieee1275/ofpath.c b/util/ieee1275/ofpath.c index 20a571191..69319d58a 100644 --- a/util/ieee1275/ofpath.c +++ b/util/ieee1275/ofpath.c @@ -85,8 +85,8 @@ trim_newline (char *path) #define OF_PATH_MAX 256 -static void -find_obppath(char *of_path, const char *sysfs_path_orig) +static int +find_obppath (char *of_path, const char *sysfs_path_orig) { char *sysfs_path, *path; @@ -108,8 +108,14 @@ find_obppath(char *of_path, const char *sysfs_path_orig) { kill_trailing_dir(sysfs_path); if (!strcmp(sysfs_path, "/sys")) - grub_util_error(_("'obppath' not found in parent dirs of %s"), - sysfs_path_orig); + { + grub_util_info (_("'obppath' not found in parent dirs of %s," + " no IEEE1275 name discovery"), + sysfs_path_orig); + free (path); + free (sysfs_path); + return 1; + } continue; } memset(of_path, 0, OF_PATH_MAX); @@ -122,6 +128,7 @@ find_obppath(char *of_path, const char *sysfs_path_orig) free (path); free (sysfs_path); + return 0; } static void @@ -159,14 +166,15 @@ trailing_digits (const char *p) return end + 1; } -static void +static int __of_path_common(char *of_path, char *sysfs_path, const char *device, int devno) { const char *digit_string; char disk[64]; - find_obppath(of_path, sysfs_path); + if (find_obppath(of_path, sysfs_path)) + return 1; digit_string = trailing_digits (device); if (*digit_string == '\0') @@ -181,6 +189,7 @@ __of_path_common(char *of_path, char *sysfs_path, sprintf(disk, "/disk@%d:%c", devno, 'a' + (part - 1)); } strcat(of_path, disk); + return 0; } static char * @@ -198,7 +207,7 @@ get_basename(char *p) return ret; } -static void +static int of_path_of_vdisk(char *of_path, const char *devname __attribute__((unused)), const char *device, @@ -207,18 +216,20 @@ of_path_of_vdisk(char *of_path, { char *sysfs_path, *p; int devno, junk; + int ret; sysfs_path = xmalloc (PATH_MAX); block_device_get_sysfs_path_and_link(devicenode, sysfs_path, PATH_MAX); p = get_basename (sysfs_path); sscanf(p, "vdc-port-%d-%d", &devno, &junk); - __of_path_common(of_path, sysfs_path, device, devno); + ret = __of_path_common(of_path, sysfs_path, device, devno); free (sysfs_path); + return ret; } -static void +static int of_path_of_ide(char *of_path, const char *devname __attribute__((unused)), const char *device, const char *devnode __attribute__((unused)), @@ -226,6 +237,7 @@ of_path_of_ide(char *of_path, { char *sysfs_path, *p; int chan, devno; + int ret; sysfs_path = xmalloc (PATH_MAX); block_device_get_sysfs_path_and_link(devicenode, @@ -233,9 +245,10 @@ of_path_of_ide(char *of_path, p = get_basename (sysfs_path); sscanf(p, "%d.%d", &chan, &devno); - __of_path_common(of_path, sysfs_path, device, devno); + ret = __of_path_common(of_path, sysfs_path, device, devno); free (sysfs_path); + return ret; } static int @@ -258,10 +271,13 @@ vendor_is_ATA(const char *path) close(fd); - free (buf); - if (!strncmp(buf, "ATA", 3)) - return 1; + { + free (buf); + return 1; + } + + free (buf); return 0; } @@ -302,7 +318,7 @@ check_sas (char *sysfs_path, int *tgt) close (fd); } -static void +static int of_path_of_scsi(char *of_path, const char *devname __attribute__((unused)), const char *device, const char *devnode __attribute__((unused)), @@ -311,6 +327,7 @@ of_path_of_scsi(char *of_path, const char *p, *digit_string, *disk_name; int host, bus, tgt, lun; char *sysfs_path, disk[64]; + int ret; sysfs_path = xmalloc (PATH_MAX); @@ -322,13 +339,15 @@ of_path_of_scsi(char *of_path, if (vendor_is_ATA(sysfs_path)) { - __of_path_common(of_path, sysfs_path, device, tgt); + ret = __of_path_common(of_path, sysfs_path, device, tgt); free (sysfs_path); - return; + return ret; } - find_obppath(of_path, sysfs_path); + ret = find_obppath(of_path, sysfs_path); free (sysfs_path); + if (ret) + return 1; if (strstr (of_path, "qlc")) strcat (of_path, "/fp@0,0"); @@ -351,6 +370,7 @@ of_path_of_scsi(char *of_path, sprintf(disk, "/%s@%x,%d:%c", disk_name, tgt, lun, 'a' + (part - 1)); } strcat(of_path, disk); + return 0; } static char * @@ -374,6 +394,7 @@ char * grub_util_devname_to_ofpath (const char *devname) { char *name_buf, *device, *devnode, *devicenode, *ofpath; + int ret; name_buf = xmalloc (PATH_MAX); name_buf = realpath (devname, name_buf); @@ -387,25 +408,37 @@ grub_util_devname_to_ofpath (const char *devname) ofpath = xmalloc (OF_PATH_MAX); if (device[0] == 'h' && device[1] == 'd') - of_path_of_ide(ofpath, name_buf, device, devnode, devicenode); + ret = of_path_of_ide(ofpath, name_buf, device, devnode, devicenode); else if (device[0] == 's' && (device[1] == 'd' || device[1] == 'r')) - of_path_of_scsi(ofpath, name_buf, device, devnode, devicenode); + ret = of_path_of_scsi(ofpath, name_buf, device, devnode, devicenode); else if (device[0] == 'v' && device[1] == 'd' && device[2] == 'i' && device[3] == 's' && device[4] == 'k') - of_path_of_vdisk(ofpath, name_buf, device, devnode, devicenode); + ret = of_path_of_vdisk(ofpath, name_buf, device, devnode, devicenode); else if (device[0] == 'f' && device[1] == 'd' && device[2] == '0' && device[3] == '\0') /* All the models I've seen have a devalias "floppy". New models have no floppy at all. */ - strcpy (ofpath, "floppy"); + { + strcpy (ofpath, "floppy"); + ret = 0; + } else - grub_util_error (_("unknown device type %s\n"), device); + { + grub_util_warn (_("unknown device type %s\n"), device); + return NULL; + } free (devnode); free (devicenode); free (name_buf); + if (ret) + { + free (ofpath); + return NULL; + } + return ofpath; }