diff options
author | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-16 20:17:58 +0000 |
---|---|---|
committer | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-16 20:17:58 +0000 |
commit | 431d0a145888ee2ff1cf4aff277e78fd36924f6b (patch) | |
tree | 8f491756731bf54e280da47ae05e8c8ec69d14d3 /base | |
parent | 6361c61b55261a9d3b43c9991e1811ce01a89714 (diff) | |
download | chromium_src-431d0a145888ee2ff1cf4aff277e78fd36924f6b.zip chromium_src-431d0a145888ee2ff1cf4aff277e78fd36924f6b.tar.gz chromium_src-431d0a145888ee2ff1cf4aff277e78fd36924f6b.tar.bz2 |
Fix some potential Mach port leaks from mach_host_self using a new ScopedMachPort class.
BUG=119379
TEST=none
Review URL: https://chromiumcodereview.appspot.com/11188003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@162225 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gypi | 3 | ||||
-rw-r--r-- | base/mac/scoped_mach_port.cc | 20 | ||||
-rw-r--r-- | base/mac/scoped_mach_port.h | 42 | ||||
-rw-r--r-- | base/process_util_mac.mm | 3 | ||||
-rw-r--r-- | base/sys_info_ios.mm | 4 | ||||
-rw-r--r-- | base/sys_info_mac.cc | 4 |
6 files changed, 73 insertions, 3 deletions
diff --git a/base/base.gypi b/base/base.gypi index a1e005e..355a071 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -216,6 +216,8 @@ 'mac/scoped_cftyperef.h', 'mac/scoped_ioobject.h', 'mac/scoped_launch_data.h', + 'mac/scoped_mach_port.cc', + 'mac/scoped_mach_port.h', 'mac/scoped_nsautorelease_pool.h', 'mac/scoped_nsautorelease_pool.mm', 'mac/scoped_nsexception_enabler.h', @@ -620,6 +622,7 @@ ['include', '^mac/foundation_util\\.'], ['include', '^mac/mac_logging\\.'], ['include', '^mac/objc_property_releaser\\.'], + ['include', '^mac/scoped_mach_port\\.'], ['include', '^mac/scoped_nsautorelease_pool\\.'], ['include', '^message_pump_mac\\.'], ['include', '^threading/platform_thread_mac\\.'], diff --git a/base/mac/scoped_mach_port.cc b/base/mac/scoped_mach_port.cc new file mode 100644 index 0000000..652e3f4 --- /dev/null +++ b/base/mac/scoped_mach_port.cc @@ -0,0 +1,20 @@ +// 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/mac/scoped_mach_port.h" + +namespace base { +namespace mac { + +ScopedMachPort::ScopedMachPort(mach_port_t port) : port_(port) { +} + +ScopedMachPort::~ScopedMachPort() { + if (port_ != MACH_PORT_NULL) { + mach_port_deallocate(mach_task_self(), port_); + } +} + +} // namespace mac +} // namespace base diff --git a/base/mac/scoped_mach_port.h b/base/mac/scoped_mach_port.h new file mode 100644 index 0000000..d2aa2f7 --- /dev/null +++ b/base/mac/scoped_mach_port.h @@ -0,0 +1,42 @@ +// 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_MAC_SCOPED_MACH_PORT_H_ +#define BASE_MAC_SCOPED_MACH_PORT_H_ + +#include <mach/mach.h> + +#include "base/basictypes.h" +#include "base/base_export.h" + +namespace base { +namespace mac { + +// A class for managing the life of a Mach port, releasing via +// mach_port_deallocate either its send and/or receive rights. +class BASE_EXPORT ScopedMachPort { + public: + // Creates a scoper by taking ownership of the port. + explicit ScopedMachPort(mach_port_t port); + + ~ScopedMachPort(); + + operator mach_port_t() const { + return port_; + } + + mach_port_t get() const { + return port_; + } + + private: + mach_port_t port_; + + DISALLOW_COPY_AND_ASSIGN(ScopedMachPort); +}; + +} // namespace mac +} // namespace base + +#endif // BASE_MAC_SCOPED_MACH_PORT_H_ diff --git a/base/process_util_mac.mm b/base/process_util_mac.mm index 4885e4b..a81625d 100644 --- a/base/process_util_mac.mm +++ b/base/process_util_mac.mm @@ -34,6 +34,7 @@ #include "base/lazy_instance.h" #include "base/logging.h" #include "base/mac/mac_util.h" +#include "base/mac/scoped_mach_port.h" #include "base/string_util.h" #include "base/sys_info.h" #include "base/threading/thread_local.h" @@ -468,7 +469,7 @@ mach_port_t ProcessMetrics::TaskForPid(ProcessHandle process) const { // Bytes committed by the system. size_t GetSystemCommitCharge() { - host_name_port_t host = mach_host_self(); + base::mac::ScopedMachPort host(mach_host_self()); mach_msg_type_number_t count = HOST_VM_INFO_COUNT; vm_statistics_data_t data; kern_return_t kr = host_statistics(host, HOST_VM_INFO, diff --git a/base/sys_info_ios.mm b/base/sys_info_ios.mm index 8b1ffd4..1ee0e54 100644 --- a/base/sys_info_ios.mm +++ b/base/sys_info_ios.mm @@ -10,6 +10,7 @@ #include <sys/types.h> #include "base/logging.h" +#include "base/mac/scoped_mach_port.h" #include "base/mac/scoped_nsautorelease_pool.h" #include "base/sys_string_conversions.h" @@ -55,7 +56,8 @@ void SysInfo::OperatingSystemVersionNumbers(int32* major_version, int64 SysInfo::AmountOfPhysicalMemory() { struct host_basic_info hostinfo; mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; - int result = host_info(mach_host_self(), + base::mac::ScopedMachPort host(mach_host_self()); + int result = host_info(host, HOST_BASIC_INFO, reinterpret_cast<host_info_t>(&hostinfo), &count); diff --git a/base/sys_info_mac.cc b/base/sys_info_mac.cc index 3a93689..8ed0290 100644 --- a/base/sys_info_mac.cc +++ b/base/sys_info_mac.cc @@ -12,6 +12,7 @@ #include <sys/types.h> #include "base/logging.h" +#include "base/mac/scoped_mach_port.h" #include "base/stringprintf.h" namespace base { @@ -44,7 +45,8 @@ void SysInfo::OperatingSystemVersionNumbers(int32* major_version, int64 SysInfo::AmountOfPhysicalMemory() { struct host_basic_info hostinfo; mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; - int result = host_info(mach_host_self(), + base::mac::ScopedMachPort host(mach_host_self()); + int result = host_info(host, HOST_BASIC_INFO, reinterpret_cast<host_info_t>(&hostinfo), &count); |