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

Unified Diff: base/win/scoped_process_information.h

Issue 9700038: ScopedProcessInformation protects against process/thread handle leaks from CreateProcess calls. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove changes from another CL. 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.h
diff --git a/base/win/scoped_process_information.h b/base/win/scoped_process_information.h
new file mode 100644
index 0000000000000000000000000000000000000000..711b0cc71d4259ee7049887a73d5da2c62ab8c49
--- /dev/null
+++ b/base/win/scoped_process_information.h
@@ -0,0 +1,85 @@
+// 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.
+
+#ifndef BASE_WIN_SCOPED_PROCESS_INFORMATION_H_
+#define BASE_WIN_SCOPED_PROCESS_INFORMATION_H_
+#pragma once
+
+#include <windows.h>
+
+#include "base/win/scoped_handle.h"
+
+namespace base {
+namespace win {
+
+class ProcessInfoTraits {
+ public:
+ typedef PROCESS_INFORMATION Handle;
+
+ // Releases the PROCESS_INFORMATION's process and thread handles.
+ static bool CloseHandle(PROCESS_INFORMATION handle) {
alexeypa (please no reviews) 2012/03/15 02:49:16 You really want "PROCESS_INFORMATION& handle" here
erikwright (departed) 2012/03/15 03:29:17 Well, the GenericScopedHandle is not expecting thi
+ bool ret = true;
+ if (handle.hThread && !::CloseHandle(handle.hThread))
+ ret = false;
+ if (handle.hProcess && !::CloseHandle(handle.hProcess))
+ ret = false;
+ handle = NullHandle();
alexeypa (please no reviews) 2012/03/15 02:49:16 |handle| is a local copy on the stack. This does n
erikwright (departed) 2012/03/15 03:29:17 Done.
+ return ret;
+ }
+
+ static bool IsHandleValid(PROCESS_INFORMATION handle) {
alexeypa (please no reviews) 2012/03/15 02:49:16 const PROCESS_INFORMATION&
erikwright (departed) 2012/03/15 03:29:17 Done.
+ return handle.hThread || handle.hProcess ||
+ handle.dwProcessId || handle.dwThreadId;
+ }
+
+ static bool IsSame(PROCESS_INFORMATION lhs, PROCESS_INFORMATION rhs) {
alexeypa (please no reviews) 2012/03/15 02:49:16 const PROCESS_INFORMATION&
erikwright (departed) 2012/03/15 03:29:17 Done.
+ return lhs.hProcess == rhs.hProcess &&
+ lhs.hThread == rhs.hThread &&
+ lhs.dwProcessId == rhs.dwProcessId &&
+ lhs.dwThreadId == rhs.dwThreadId;
+ }
+
+ static PROCESS_INFORMATION NullHandle() {
alexeypa (please no reviews) 2012/03/15 02:49:16 I don't think returning raw structures like this i
erikwright (departed) 2012/03/15 03:29:17 Hmm. Then the client needs to have both a PROCESS_
alexeypa (please no reviews) 2012/03/15 16:38:27 After a second thought, wrapping a pointer does no
+ PROCESS_INFORMATION null_process_information = {0};
+ return null_process_information;
+ }
+};
+
+// Manages the closing of process and thread handles from PROCESS_INFORMATION
+// structures. Allows clients to take ownership of either handle independently.
+class ScopedProcessInformation : public GenericScopedHandle<ProcessInfoTraits> {
grt (UTC plus 2) 2012/03/15 02:59:30 did you consider something like this: class Scope
erikwright (departed) 2012/03/15 03:29:17 WRT Get(), it could also just be process_handle()
grt (UTC plus 2) 2012/03/15 18:01:09 It might be a can of worms, but you could: Handle*
+ public:
+ ScopedProcessInformation()
+ : GenericScopedHandle(ProcessInfoTraits::NullHandle()) {}
+
+ explicit ScopedProcessInformation(Handle handle)
+ : GenericScopedHandle(handle) {}
+
+ // Transfers ownership of the process handle away from this object. The
+ // hProcess and dwProcessId members will be reset.
+ HANDLE TakeProcessHandle() {
+ PROCESS_INFORMATION process_info = Take();
+ HANDLE process = process_info.hProcess;
+ process_info.hProcess = NULL;
+ process_info.dwProcessId = 0;
+ Set(process_info);
alexeypa (please no reviews) 2012/03/15 16:38:27 I would just modified the structure directly here
erikwright (departed) 2012/03/15 18:02:44 It's a private member. I'd rather not make it prot
+ return process;
+ }
+
+ // Transfers ownership of the thread handle away from this object. The hThread
+ // and dwThreadId members will be reset.
+ HANDLE TakeThreadHandle() {
+ PROCESS_INFORMATION process_info = Take();
+ HANDLE thread = process_info.hThread;
+ process_info.hThread = NULL;
+ process_info.dwThreadId = 0;
alexeypa (please no reviews) 2012/03/15 16:38:27 Same as above.
+ Set(process_info);
+ return thread;
+ }
+};
+
+} // namespace win
+} // namespace base
+
+#endif // BASE_SCOPED_HANDLE_WIN_H_

Powered by Google App Engine
This is Rietveld 408576698