diff options
author | Jamie Liu <jamieliu@google.com> | 2018-12-18 07:22:44 -0800 |
---|---|---|
committer | Shentubot <shentubot@google.com> | 2018-12-18 07:23:53 -0800 |
commit | e7b47844d969673cec06ea745d577155131ecf3b (patch) | |
tree | ed2c20be75354f2e577a85ccd213f8da6d2d214b /test/util/proc_util.cc | |
parent | 12c7430a01ad2b484987dd8ee24b6f2907e7366d (diff) |
Correctly handle filenames containing spaces in ParseProcMapsLine.
PiperOrigin-RevId: 225992500
Change-Id: Icc8b1675f1cb625fc5e8ef7389beb42fa7bfaa13
Diffstat (limited to 'test/util/proc_util.cc')
-rw-r--r-- | test/util/proc_util.cc | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/test/util/proc_util.cc b/test/util/proc_util.cc index 72f7e67d0..7ebce0759 100644 --- a/test/util/proc_util.cc +++ b/test/util/proc_util.cc @@ -17,6 +17,7 @@ #include <algorithm> #include <vector> +#include "absl/strings/ascii.h" #include "absl/strings/str_cat.h" #include "absl/strings/str_split.h" #include "absl/strings/string_view.h" @@ -29,10 +30,15 @@ namespace testing { // Parses a single line from /proc/<xxx>/maps. PosixErrorOr<ProcMapsEntry> ParseProcMapsLine(absl::string_view line) { ProcMapsEntry map_entry = {}; - std::vector<std::string> parts = absl::StrSplit(line, ' ', absl::SkipEmpty()); - // A size of 5 means there is no file name specified. - if (parts.size() != 5 && parts.size() != 6) { + // Limit splitting to 6 parts so that if there is a file path and it contains + // spaces, the file path is not split. + std::vector<std::string> parts = + absl::StrSplit(line, absl::MaxSplits(' ', 5), absl::SkipEmpty()); + + // parts.size() should be 6 if there is a file name specified, and 5 + // otherwise. + if (parts.size() < 5) { return PosixError(EINVAL, absl::StrCat("Invalid line: ", line)); } @@ -67,8 +73,9 @@ PosixErrorOr<ProcMapsEntry> ParseProcMapsLine(absl::string_view line) { ASSIGN_OR_RETURN_ERRNO(map_entry.inode, Atoi<int64_t>(parts[4])); if (parts.size() == 6) { - // A filename is present. - map_entry.filename = parts[5]; + // A filename is present. However, absl::StrSplit retained the whitespace + // between the inode number and the filename. + map_entry.filename = std::string(absl::StripLeadingAsciiWhitespace(parts[5])); } return map_entry; |