diff options
author | Alban Crequy | 2018-06-22 13:54:25 +0200 |
---|---|---|
committer | Karel Zak | 2018-07-10 13:48:38 +0200 |
commit | 18a52a5094f820b5da013daf5972eb8e65be9680 (patch) | |
tree | f00518c084e8e7dc0a30b222e211ad1cb4d99c52 | |
parent | lsblk: add PATH column (diff) | |
download | kernel-qcow2-util-linux-18a52a5094f820b5da013daf5972eb8e65be9680.tar.gz kernel-qcow2-util-linux-18a52a5094f820b5da013daf5972eb8e65be9680.tar.xz kernel-qcow2-util-linux-18a52a5094f820b5da013daf5972eb8e65be9680.zip |
libmount: (mountinfo) parse empty strings in source
The source of a mount in /proc/self/mountinfo can unfortunately be an
empty string. Before this patch, 'mount' and 'mountpoint' fail as
following:
$ sudo mount -t tmpfs "" /tmp/bb
$ mount
mount: /proc/self/mountinfo: parse error at line 64 -- ignored
$ mountpoint /tmp/bb
/tmp/bb is not a mountpoint
This patch fixes the parsing. It is unfortunately more complex because
sscanf() does not handle fields with empty strings easily.
Other projects have their own parser for mountinfo and have similar
issues. I know of runc and runtime-tools (I'll send a patch for those
two) but there are probably others.
Signed-off-by: Alban Crequy <alban@kinvolk.io>
-rw-r--r-- | libmount/src/tab_parse.c | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/libmount/src/tab_parse.c b/libmount/src/tab_parse.c index 3ed84ebc2..4944d5f8c 100644 --- a/libmount/src/tab_parse.c +++ b/libmount/src/tab_parse.c @@ -173,13 +173,36 @@ static int mnt_parse_mountinfo_line(struct libmnt_fs *fs, char *s) fs->opt_fields = strndup(s + 1, p - s - 1); s = p + 3; - rc += sscanf(s, UL_SCNsA" " /* (8) FS type */ - UL_SCNsA" " /* (9) source */ - UL_SCNsA, /* (10) fs options (fs specific) */ + end = 0; + rc += sscanf(s, UL_SCNsA"%n", /* (8) FS type */ &fstype, - &src, - &fs->fs_optstr); + &end); + + if (rc >= 8 && end > 0) + s += end; + if (s[0] == ' ') + s++; + + /* (9) source can unfortunately be an empty string "" and scanf does + * not work well with empty string. Test with: + * $ sudo mount -t tmpfs "" /tmp/bb + * $ mountpoint /tmp/bb + * */ + if (s[0] == ' ') { + src = strdup(""); + s++; + rc++; + rc += sscanf(s, UL_SCNsA, /* (10) fs options (fs specific) */ + + &fs->fs_optstr); + } else { + rc += sscanf(s, UL_SCNsA" " /* (9) source */ + UL_SCNsA, /* (10) fs options (fs specific) */ + + &src, + &fs->fs_optstr); + } if (rc >= 10) { size_t sz; |