Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Unified Diff: base/files/file_util_posix.cc

Issue 2687713003: Stop base::OpenFile from leaking fds/handles into child procs. (Closed)
Patch Set: macOS fix Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/files/file_util_posix.cc
diff --git a/base/files/file_util_posix.cc b/base/files/file_util_posix.cc
index 34a1c4d1e6e2481a953ad2fac5d30ddb124ca3cd..915e6fe7f057e1467bfe0bc69b2f078f0cd49bc9 100644
--- a/base/files/file_util_posix.cc
+++ b/base/files/file_util_posix.cc
@@ -185,6 +185,21 @@ bool DetermineDevShmExecutable() {
#endif // defined(OS_LINUX)
#endif // !defined(OS_NACL_NONSFI)
+// Appends |mode_char| to |mode| before the optional character set encoding for
+// non-Mac platforms (which do not support this); see
gab 2017/02/09 15:18:09 Link to https://developer.apple.com/legacy/library
grt (UTC plus 2) 2017/02/10 09:37:21 Done.
+// https://www.gnu.org/software/libc/manual/html_node/Opening-Streams.html for
gab 2017/02/09 15:18:09 I don't see mention of the comma @ this link.
grt (UTC plus 2) 2017/02/09 15:57:01 Search for "If the opentype string contains the se
+// details.
+std::string AppendModeCharacter(StringPiece mode, char mode_char) {
+ // Add |mode_char| immediately before the comma or at the end of the string.
+ std::string result(mode.as_string());
+#if !defined(OS_MACOSX)
+ size_t comma_pos = result.find(L',');
+ result.insert(comma_pos == std::string::npos ? result.length() : comma_pos, 1,
+ mode_char);
+#endif
+ return result;
+}
+
} // namespace
#if !defined(OS_NACL_NONSFI)
@@ -710,11 +725,22 @@ bool GetFileInfo(const FilePath& file_path, File::Info* results) {
#endif // !defined(OS_NACL_NONSFI)
FILE* OpenFile(const FilePath& filename, const char* mode) {
+ // 'e' is unconditionally added below, so be sure there is not one already
+ // present before a comma in |mode|.
+ DCHECK(::strchr(mode, 'e') == nullptr ||
+ (::strchr(mode, ',') != nullptr &&
+ ::strchr(mode, 'e') > ::strchr(mode, ',')));
ThreadRestrictions::AssertIOAllowed();
FILE* result = NULL;
+ std::string mode_with_e(AppendModeCharacter(mode, 'e'));
do {
- result = fopen(filename.value().c_str(), mode);
+ result = fopen(filename.value().c_str(), mode_with_e.c_str());
} while (!result && errno == EINTR);
+#if defined(OS_MACOSX)
+ // Mark the descriptor as close-on-exec.
+ if (result)
+ SetCloseOnExec(fileno(result));
+#endif
return result;
}

Powered by Google App Engine
This is Rietveld 408576698