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

Unified Diff: base/win/scoped_process_information.cc

Issue 9700038: ScopedProcessInformation protects against process/thread handle leaks from CreateProcess calls. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Touch a previously missed use of PROCESS_INFORMATION Created 8 years, 9 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/win/scoped_process_information.cc
diff --git a/base/win/scoped_process_information.cc b/base/win/scoped_process_information.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4659de1c1aaa495461e45fcf2627e6c2d64cff3e
--- /dev/null
+++ b/base/win/scoped_process_information.cc
@@ -0,0 +1,66 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/win/scoped_process_information.h"
+
+#include "base/logging.h"
+
+namespace base {
+namespace win {
+
+ScopedProcessInformation::ScopedProcessInformation()
+ : process_information_() {
+}
+
+ScopedProcessInformation::~ScopedProcessInformation() {
+ Close();
+}
+
+PROCESS_INFORMATION* ScopedProcessInformation::Receive() {
+ DCHECK(!IsValid()) << "process_information_ must be NULL";
+ return &process_information_;
+}
+
+bool ScopedProcessInformation::IsValid() const {
+ return process_information_.hThread || process_information_.hProcess ||
+ process_information_.dwProcessId || process_information_.dwThreadId;
alexeypa (please no reviews) 2012/03/29 04:51:35 I think that it should test only handles but not t
erikwright (departed) 2012/03/30 17:30:27 The only way for handle and id to not be in the sa
alexeypa (please no reviews) 2012/03/30 18:26:39 There is one example in the accompanying CL (the s
+}
+
+void ScopedProcessInformation::Close() {
+ if (process_information_.hThread)
+ ::CloseHandle(process_information_.hThread);
alexeypa (please no reviews) 2012/03/29 04:51:35 It will be useful for debugging if NOTREACHED() wi
erikwright (departed) 2012/03/30 17:30:27 Done.
+ if (process_information_.hProcess)
+ ::CloseHandle(process_information_.hProcess);
+ Reset();
+}
+
+PROCESS_INFORMATION ScopedProcessInformation::Take() {
alexeypa (please no reviews) 2012/03/29 04:51:35 Now, since ScopedProcessInformation is not limited
erikwright (departed) 2012/03/30 17:30:27 Looking through your suggestions on the use of Swa
alexeypa (please no reviews) 2012/03/30 18:26:39 The main reason to get rid of Take() is to reduce
+ PROCESS_INFORMATION process_information = process_information_;
+ Reset();
+ return process_information;
+}
+
+HANDLE ScopedProcessInformation::TakeProcessHandle() {
+ HANDLE process = process_information_.hProcess;
+ process_information_.hProcess = NULL;
+ process_information_.dwProcessId = 0;
+ return process;
+}
+
+HANDLE ScopedProcessInformation::TakeThreadHandle() {
+ HANDLE thread = process_information_.hThread;
+ process_information_.hThread = NULL;
+ process_information_.dwThreadId = 0;
+ return thread;
+}
+
+void ScopedProcessInformation::Reset() {
+ process_information_.hThread = NULL;
+ process_information_.hProcess = NULL;
+ process_information_.dwProcessId = 0;
+ process_information_.dwThreadId = 0;
+}
+
+} // namespace win
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698