Skip to contentSkip to conversation

Btrfs-progs: add missing error handling in find_mount_root()

PATCH In review [email protected] 2 participants 2 messages Jan 14, 2019 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 2/3 Btrfs-progs: add missing error handling in find_mount_root()

From: Filipe Manana <[email protected]>

The strdup() function can fail, in which case it returns NULL and sets errno [1]. Therefore add the missing error check after calling strdup() at find_mount_root().

[1] - http://man7.org/linux/man-pages/man3/strdup.3p.html

---
 utils.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
utils.c +7 -1
index 6616630b..3bc6bc3b 100644
@@ -2048,7 +2048,7 @@ int find_mount_root(const char *path, char **mount_root)
 	int fd;
 	struct mntent *ent;
 	int len;
-	int ret;
+	int ret = 0;
 	int not_btrfs = 1;
 	int longest_matchlen = 0;
 	char *longest_match = NULL;
@@ -2071,12 +2071,18 @@ int find_mount_root(const char *path, char **mount_root)
 				free(longest_match);
 				longest_matchlen = len;
 				longest_match = strdup(ent->mnt_dir);
+				if (!longest_match) {
+					ret = -errno;
+					break;
+				}
 				not_btrfs = strcmp(ent->mnt_type, "btrfs");
 			}
 		}
 	}
 	endmntent(mnttab);
 
+	if (ret)
+		return ret;
 	if (!longest_match)
 		return -ENOENT;
 	if (not_btrfs) {
Signature · 1 line
2.11.0
Reviewed-by on v1 2/3
#1 lore raw
Quoted 6 lines · On Mon, 14 Jan 2019 13:31:34 +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

The strdup() function can fail, in which case it returns NULL and sets errno [1]. Therefore add the missing error check after calling strdup() at find_mount_root().

Quoted 6 lines

[1] - http://man7.org/linux/man-pages/man3/strdup.3p.html

---
 utils.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Looks good: