From a68a6206190feae6bbe66fa336d38e405624ba07 Mon Sep 17 00:00:00 2001 From: Cheney Ni Date: Wed, 25 Mar 2020 18:37:33 +0800 Subject: [PATCH] =?utf8?q?BluetoothAudioHAL:=20Have=20full=C2=A0initializa?= =?utf8?q?tion=20for=20audio=20hardware=20objects?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Audio frameworks usually check whether a module has specific features by particular function pointers, and must be null if unsupported. If a module is not fully initialized, some pointers would be undefined, but causes abnormal crashes when accessed by audio frameworks.   Bug: 152379666 Test: manually Change-Id: Id6a79b651f2acbd35bd509ba36a80adea5c13e05 Merged-In: Id6a79b651f2acbd35bd509ba36a80adea5c13e05 (cherry picked from commit 7397c51ac706d3577b277d7b5bcdc78c78fcc6ac) --- audio_bluetooth_hw/audio_bluetooth_hw.cc | 5 +++-- audio_bluetooth_hw/stream_apis.cc | 2 +- audio_bluetooth_hw/stream_apis.h | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/audio_bluetooth_hw/audio_bluetooth_hw.cc b/audio_bluetooth_hw/audio_bluetooth_hw.cc index 5d0b43a69..887c4e3b6 100644 --- a/audio_bluetooth_hw/audio_bluetooth_hw.cc +++ b/audio_bluetooth_hw/audio_bluetooth_hw.cc @@ -102,7 +102,8 @@ static int adev_get_mic_mute(const struct audio_hw_device* dev, bool* state) { static int adev_dump(const audio_hw_device_t* device, int fd) { return 0; } static int adev_close(hw_device_t* device) { - free(device); + auto* bluetooth_device = reinterpret_cast(device); + delete bluetooth_device; return 0; } @@ -111,7 +112,7 @@ static int adev_open(const hw_module_t* module, const char* name, LOG(VERBOSE) << __func__ << ": name=[" << name << "]"; if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; - auto bluetooth_audio_device = new BluetoothAudioDevice; + auto bluetooth_audio_device = new BluetoothAudioDevice{}; struct audio_hw_device* adev = &bluetooth_audio_device->audio_device_; if (!adev) return -ENOMEM; diff --git a/audio_bluetooth_hw/stream_apis.cc b/audio_bluetooth_hw/stream_apis.cc index 7b01c3236..d809b57dc 100644 --- a/audio_bluetooth_hw/stream_apis.cc +++ b/audio_bluetooth_hw/stream_apis.cc @@ -635,7 +635,7 @@ int adev_open_output_stream(struct audio_hw_device* dev, struct audio_stream_out** stream_out, const char* address __unused) { *stream_out = nullptr; - auto* out = new BluetoothStreamOut; + auto* out = new BluetoothStreamOut{}; if (!out->bluetooth_output_.SetUp(devices)) { delete out; return -EINVAL; diff --git a/audio_bluetooth_hw/stream_apis.h b/audio_bluetooth_hw/stream_apis.h index e319d95e2..c894d1e4b 100644 --- a/audio_bluetooth_hw/stream_apis.h +++ b/audio_bluetooth_hw/stream_apis.h @@ -46,7 +46,7 @@ std::ostream& operator<<(std::ostream& os, const BluetoothStreamState& state); struct BluetoothStreamOut { // Must be the first member so it can be cast from audio_stream // or audio_stream_out pointer - audio_stream_out stream_out_; + audio_stream_out stream_out_{}; ::android::bluetooth::audio::BluetoothAudioPortOut bluetooth_output_; int64_t last_write_time_us_; // Audio PCM Configs @@ -66,7 +66,7 @@ struct BluetoothStreamOut { struct BluetoothAudioDevice { // Important: device must be first as an audio_hw_device* may be cast to // BluetoothAudioDevice* when the type is implicitly known. - audio_hw_device audio_device_; + audio_hw_device audio_device_{}; // protect against device->output and stream_out from being inconsistent std::mutex mutex_; std::list opened_stream_outs_ = -- 2.11.0