From e97b5493a5fb1190396f2e7ced0bff6f60a9793d Mon Sep 17 00:00:00 2001 From: henrika Date: Fri, 1 Jun 2018 13:13:03 +0200 Subject: [PATCH] Fixes leak of AudioDeviceID array due to early return in AudioDeviceMac::GetNumberDevices() Bug: webrtc:9348 Change-Id: I67a534ec8225180aa67018f7c11f1983262af585 Reviewed-on: https://webrtc-review.googlesource.com/80480 Commit-Queue: Henrik Andreassson Reviewed-by: Karl Wiberg Cr-Commit-Position: refs/heads/master@{#23490} --- modules/audio_device/mac/audio_device_mac.cc | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/modules/audio_device/mac/audio_device_mac.cc b/modules/audio_device/mac/audio_device_mac.cc index c585c32caf..ed490c6d95 100644 --- a/modules/audio_device/mac/audio_device_mac.cc +++ b/modules/audio_device/mac/audio_device_mac.cc @@ -14,6 +14,7 @@ #include "rtc_base/arraysize.h" #include "rtc_base/checks.h" #include "rtc_base/platform_thread.h" +#include "rtc_base/ptr_util.h" #include "system_wrappers/include/event_wrapper.h" #include @@ -1564,8 +1565,8 @@ int32_t AudioDeviceMac::GetNumberDevices(const AudioObjectPropertyScope scope, return 0; } - AudioDeviceID* deviceIds = (AudioDeviceID*)malloc(size); UInt32 numberDevices = size / sizeof(AudioDeviceID); + const auto deviceIds = rtc::MakeUnique(numberDevices); AudioBufferList* bufferList = NULL; UInt32 numberScopedDevices = 0; @@ -1596,8 +1597,9 @@ int32_t AudioDeviceMac::GetNumberDevices(const AudioObjectPropertyScope scope, // Then list the rest of the devices bool listOK = true; - WEBRTC_CA_LOG_ERR(AudioObjectGetPropertyData( - kAudioObjectSystemObject, &propertyAddress, 0, NULL, &size, deviceIds)); + WEBRTC_CA_LOG_ERR(AudioObjectGetPropertyData(kAudioObjectSystemObject, + &propertyAddress, 0, NULL, &size, + deviceIds.get())); if (err != noErr) { listOK = false; } else { @@ -1641,25 +1643,13 @@ int32_t AudioDeviceMac::GetNumberDevices(const AudioObjectPropertyScope scope, } if (!listOK) { - if (deviceIds) { - free(deviceIds); - deviceIds = NULL; - } - if (bufferList) { free(bufferList); bufferList = NULL; } - return -1; } - // Happy ending - if (deviceIds) { - free(deviceIds); - deviceIds = NULL; - } - return numberScopedDevices; }