[CRIU] [PATCH 2/5] parse_mountinfo: fix and simplify the usage of r_fstype

Oleg Nesterov oleg at redhat.com
Sun Mar 29 10:24:00 PDT 2015


1. parse_mountinfo() forgets to free(fst) if parse_mountinfo_ent()
   succeeds.

2. The usage of fst/r_fstype is ovecomplicated for no reason.

Just change the parse_mountinfo() paths to populate/use/free this
fsname unconditionally, and move the ownership to the caller. There
is no reason to check FSTYPE__UNSUPPORTED and/or fallback to ->name.

Better yet, we could even turn fsname into the local "char []" and
avoid %ms and free(), but then we would need to pass the length of
this buffer to parse_mountinfo_ent().

Signed-off-by: Oleg Nesterov <oleg at redhat.com>
---
 proc_parse.c |   22 ++++++++++------------
 1 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/proc_parse.c b/proc_parse.c
index a06a65a..a00bcbb 100644
--- a/proc_parse.c
+++ b/proc_parse.c
@@ -918,12 +918,11 @@ static int parse_mnt_opt(char *str, struct mount_info *mi, int *off)
 	return 0;
 }
 
-static int parse_mountinfo_ent(char *str, struct mount_info *new, char **r_fstype)
+static int parse_mountinfo_ent(char *str, struct mount_info *new, char **fsname)
 {
 	unsigned int kmaj, kmin;
 	int ret, n;
 	char *opt;
-	char *fstype;
 
 	new->mountpoint = xmalloc(PATH_MAX);
 	if (new->mountpoint == NULL)
@@ -953,15 +952,13 @@ static int parse_mountinfo_ent(char *str, struct mount_info *new, char **r_fstyp
 		return -1;
 
 	str += n;
-	ret = sscanf(str, "%ms %ms %ms", &fstype, &new->source, &opt);
+	ret = sscanf(str, "%ms %ms %ms", fsname, &new->source, &opt);
 	if (ret != 3)
 		return -1;
 
-	ret = -1;
-	new->fstype = find_fstype_by_name(fstype);
-	if (new->fstype->code == FSTYPE__UNSUPPORTED)
-		*r_fstype = fstype; /* keep for logging */
+	new->fstype = find_fstype_by_name(*fsname);
 
+	ret = -1;
 	new->options = xmalloc(strlen(opt) + 1);
 	if (!new->options)
 		goto err;
@@ -972,8 +969,6 @@ static int parse_mountinfo_ent(char *str, struct mount_info *new, char **r_fstyp
 	ret = 0;
 err:
 	free(opt);
-	if (!*r_fstype)
-		free(fstype);
 	return ret;
 }
 
@@ -992,7 +987,7 @@ struct mount_info *parse_mountinfo(pid_t pid, struct ns_id *nsid)
 	while (fgets(str, sizeof(str), f)) {
 		struct mount_info *new;
 		int ret = -1;
-		char *fst = NULL;
+		char *fsname = NULL;
 
 		new = mnt_entry_alloc();
 		if (!new)
@@ -1000,14 +995,14 @@ struct mount_info *parse_mountinfo(pid_t pid, struct ns_id *nsid)
 
 		new->nsid = nsid;
 
-		ret = parse_mountinfo_ent(str, new, &fst);
+		ret = parse_mountinfo_ent(str, new, &fsname);
 		if (ret < 0) {
 			pr_err("Bad format in %d mountinfo\n", pid);
 			goto end;
 		}
 
 		pr_info("\ttype %s source %s mnt_id %d s_dev %#x %s @ %s flags %#x options %s\n",
-				fst ? : new->fstype->name, new->source,
+				fsname, new->source,
 				new->mnt_id, new->s_dev, new->root, new->mountpoint,
 				new->flags, new->options);
 
@@ -1020,6 +1015,9 @@ struct mount_info *parse_mountinfo(pid_t pid, struct ns_id *nsid)
 			}
 		}
 end:
+		if (fsname)
+			free(fsname);
+
 		if (new) {
 			new->next = list;
 			list = new;
-- 
1.5.5.1



More information about the CRIU mailing list