From cba5a85a67f879b6431833e87ceb93d44b8ac48a Mon Sep 17 00:00:00 2001 From: Vladimir Serbinenko Date: Mon, 26 Jan 2015 09:51:48 +0100 Subject: [PATCH] linux/getroot: Fix error handling. Found by: Coverity scan. --- grub-core/osdep/linux/getroot.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/grub-core/osdep/linux/getroot.c b/grub-core/osdep/linux/getroot.c index 7007193d9..f48b81aed 100644 --- a/grub-core/osdep/linux/getroot.c +++ b/grub-core/osdep/linux/getroot.c @@ -226,7 +226,7 @@ grub_find_root_devices_from_btrfs (const char *dir) char **ret; fd = open (dir, 0); - if (!fd) + if (fd < 0) return NULL; if (ioctl (fd, BTRFS_IOC_FS_INFO, &fsi) < 0) @@ -274,11 +274,11 @@ get_btrfs_fs_prefix (const char *mount_path) args.objectid = GRUB_BTRFS_TREE_ROOT_OBJECTID; if (ioctl (fd, BTRFS_IOC_INO_LOOKUP, &args) < 0) - return NULL; + goto fail; tree_id = args.treeid; if (fstat (fd, &st) < 0) - return NULL; + goto fail; inode_id = st.st_ino; while (tree_id != GRUB_BTRFS_ROOT_VOL_OBJECTID @@ -309,11 +309,11 @@ get_btrfs_fs_prefix (const char *mount_path) sargs.key.nr_items = 1; if (ioctl (fd, BTRFS_IOC_TREE_SEARCH, &sargs) < 0) - return NULL; + goto fail; if (sargs.key.nr_items == 0) - return NULL; - + goto fail; + tree_id = sargs.buf[2]; br = (struct grub_btrfs_root_backref *) (sargs.buf + 4); inode_id = br->inode_id; @@ -336,10 +336,10 @@ get_btrfs_fs_prefix (const char *mount_path) sargs.key.max_type = GRUB_BTRFS_ITEM_TYPE_INODE_REF; if (ioctl (fd, BTRFS_IOC_TREE_SEARCH, &sargs) < 0) - return NULL; + goto fail; if (sargs.key.nr_items == 0) - return NULL; + goto fail; inode_id = sargs.buf[2]; @@ -362,6 +362,10 @@ get_btrfs_fs_prefix (const char *mount_path) if (!ret) return xstrdup ("/"); return ret; + fail: + free (ret); + close (fd); + return NULL; } @@ -688,7 +692,7 @@ char * grub_util_part_to_disk (const char *os_dev, struct stat *st, int *is_part) { - char *path = xmalloc (PATH_MAX); + char *path; if (! S_ISBLK (st->st_mode)) { @@ -696,6 +700,8 @@ grub_util_part_to_disk (const char *os_dev, struct stat *st, return xstrdup (os_dev); } + path = xmalloc (PATH_MAX); + if (! realpath (os_dev, path)) return NULL;