From 1abcb1106c58e2882ed91a0ecfc6d77cfeacfdfa Mon Sep 17 00:00:00 2001 From: Byoungchan Lee Date: Thu, 9 Jun 2022 18:14:00 +0900 Subject: [PATCH] Remove usage of sprintf in modules sprintf is marked as deprecated with Xcode 14. Bug: chromium:1331345 Change-Id: I834f392bee96e6b6725d5aee469a243dbc6e272e Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/265521 Reviewed-by: Henrik Andreassson Reviewed-by: Henrik Lundin Commit-Queue: Henrik Lundin Cr-Commit-Position: refs/heads/main@{#37162} --- modules/audio_coding/test/Channel.cc | 7 +++--- modules/audio_device/BUILD.gn | 5 +++- modules/audio_device/mac/audio_device_mac.cc | 24 ++++++++++++-------- modules/audio_device/mac/audio_device_mac.h | 3 ++- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/modules/audio_coding/test/Channel.cc b/modules/audio_coding/test/Channel.cc index b85f9f416a..35aa6cb6b4 100644 --- a/modules/audio_coding/test/Channel.cc +++ b/modules/audio_coding/test/Channel.cc @@ -12,6 +12,7 @@ #include +#include "rtc_base/strings/string_builder.h" #include "rtc_base/time_utils.h" namespace webrtc { @@ -217,9 +218,9 @@ Channel::Channel(int16_t chID) } if (chID >= 0) { _saveBitStream = true; - char bitStreamFileName[500]; - sprintf(bitStreamFileName, "bitStream_%d.dat", chID); - _bitStreamFile = fopen(bitStreamFileName, "wb"); + rtc::StringBuilder ss; + ss.AppendFormat("bitStream_%d.dat", chID); + _bitStreamFile = fopen(ss.str().c_str(), "wb"); } else { _saveBitStream = false; } diff --git a/modules/audio_device/BUILD.gn b/modules/audio_device/BUILD.gn index 61b2dfd767..0a67a4b8d6 100644 --- a/modules/audio_device/BUILD.gn +++ b/modules/audio_device/BUILD.gn @@ -208,7 +208,10 @@ rtc_library("audio_device_impl") { "../../system_wrappers:metrics", "../utility", ] - absl_deps = [ "//third_party/abseil-cpp/absl/base:core_headers" ] + absl_deps = [ + "//third_party/abseil-cpp/absl/base:core_headers", + "//third_party/abseil-cpp/absl/strings:strings", + ] if (rtc_include_internal_audio_device && is_ios) { deps += [ "../../sdk:audio_device" ] } diff --git a/modules/audio_device/mac/audio_device_mac.cc b/modules/audio_device/mac/audio_device_mac.cc index 2aa2841c01..462287a27d 100644 --- a/modules/audio_device/mac/audio_device_mac.cc +++ b/modules/audio_device/mac/audio_device_mac.cc @@ -835,7 +835,8 @@ int32_t AudioDeviceMac::PlayoutDeviceName(uint16_t index, memset(guid, 0, kAdmMaxGuidSize); } - return GetDeviceName(kAudioDevicePropertyScopeOutput, index, name); + return GetDeviceName(kAudioDevicePropertyScopeOutput, index, + rtc::ArrayView(name, kAdmMaxDeviceNameSize)); } int32_t AudioDeviceMac::RecordingDeviceName(uint16_t index, @@ -853,7 +854,8 @@ int32_t AudioDeviceMac::RecordingDeviceName(uint16_t index, memset(guid, 0, kAdmMaxGuidSize); } - return GetDeviceName(kAudioDevicePropertyScopeInput, index, name); + return GetDeviceName(kAudioDevicePropertyScopeInput, index, + rtc::ArrayView(name, kAdmMaxDeviceNameSize)); } int16_t AudioDeviceMac::RecordingDevices() { @@ -1646,9 +1648,8 @@ int32_t AudioDeviceMac::GetNumberDevices(const AudioObjectPropertyScope scope, int32_t AudioDeviceMac::GetDeviceName(const AudioObjectPropertyScope scope, const uint16_t index, - char* name) { + rtc::ArrayView name) { OSStatus err = noErr; - UInt32 len = kAdmMaxDeviceNameSize; AudioDeviceID deviceIds[MaxNumberDevices]; int numberDevices = GetNumberDevices(scope, deviceIds, MaxNumberDevices); @@ -1689,21 +1690,24 @@ int32_t AudioDeviceMac::GetDeviceName(const AudioObjectPropertyScope scope, scope, 0}; if (isDefaultDevice) { - char devName[len]; + std::array devName; + UInt32 len = devName.size(); - WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData(usedID, &propertyAddress, - 0, NULL, &len, devName)); + WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( + usedID, &propertyAddress, 0, NULL, &len, devName.data())); - sprintf(name, "default (%s)", devName); + rtc::SimpleStringBuilder ss(name); + ss.AppendFormat("default (%s)", devName.data()); } else { if (index < numberDevices) { usedID = deviceIds[index]; } else { usedID = index; } + UInt32 len = name.size(); - WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData(usedID, &propertyAddress, - 0, NULL, &len, name)); + WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( + usedID, &propertyAddress, 0, NULL, &len, name.data())); } return 0; diff --git a/modules/audio_device/mac/audio_device_mac.h b/modules/audio_device/mac/audio_device_mac.h index fe0d3beed8..bb06395d03 100644 --- a/modules/audio_device/mac/audio_device_mac.h +++ b/modules/audio_device/mac/audio_device_mac.h @@ -18,6 +18,7 @@ #include #include +#include "absl/strings/string_view.h" #include "modules/audio_device/audio_device_generic.h" #include "modules/audio_device/mac/audio_mixer_manager_mac.h" #include "rtc_base/event.h" @@ -179,7 +180,7 @@ class AudioDeviceMac : public AudioDeviceGeneric { int32_t GetDeviceName(AudioObjectPropertyScope scope, uint16_t index, - char* name); + rtc::ArrayView name); int32_t InitDevice(uint16_t userDeviceIndex, AudioDeviceID& deviceId,