From db8c002f4ed4b424c8f36da27e310fb96bb8a7c4 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Tue, 24 May 2016 11:56:22 -1000 Subject: [PATCH] Safer null-pointer handling in AudioQueueServices --- lib/audiodev/AQS.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/audiodev/AQS.cpp b/lib/audiodev/AQS.cpp index b74ebb8..38c1676 100644 --- a/lib/audiodev/AQS.cpp +++ b/lib/audiodev/AQS.cpp @@ -42,7 +42,7 @@ struct AQSAudioVoiceEngine : BaseAudioVoiceEngine AudioQueueBufferRef m_buffers[3]; size_t m_frameBytes; - MIDIClientRef m_midiClient; + MIDIClientRef m_midiClient = 0; std::mutex m_engineMutex; std::condition_variable m_engineEnterCv; @@ -115,6 +115,9 @@ struct AQSAudioVoiceEngine : BaseAudioVoiceEngine std::vector> enumerateMIDIDevices() const { + if (!m_midiClient) + return {}; + std::vector> ret; ItemCount numDevices = MIDIGetNumberOfDevices(); @@ -368,6 +371,9 @@ struct AQSAudioVoiceEngine : BaseAudioVoiceEngine std::unique_ptr newVirtualMIDIIn(ReceiveFunctor&& receiver) { + if (!m_midiClient) + return {}; + std::unique_ptr ret = std::make_unique(true, std::move(receiver)); if (!ret) return {}; @@ -387,6 +393,9 @@ struct AQSAudioVoiceEngine : BaseAudioVoiceEngine std::unique_ptr newVirtualMIDIOut() { + if (!m_midiClient) + return {}; + std::unique_ptr ret = std::make_unique(true); if (!ret) return {}; @@ -403,6 +412,9 @@ struct AQSAudioVoiceEngine : BaseAudioVoiceEngine std::unique_ptr newVirtualMIDIInOut(ReceiveFunctor&& receiver) { + if (!m_midiClient) + return {}; + std::unique_ptr ret = std::make_unique(true, std::move(receiver)); if (!ret) return {}; @@ -430,6 +442,9 @@ struct AQSAudioVoiceEngine : BaseAudioVoiceEngine std::unique_ptr newRealMIDIIn(const char* name, ReceiveFunctor&& receiver) { + if (!m_midiClient) + return {}; + MIDIEndpointRef src = LookupMIDISource(name); if (!src) return {}; @@ -454,6 +469,9 @@ struct AQSAudioVoiceEngine : BaseAudioVoiceEngine std::unique_ptr newRealMIDIOut(const char* name) { + if (!m_midiClient) + return {}; + MIDIEndpointRef dst = LookupMIDIDest(name); if (!dst) return {}; @@ -476,6 +494,9 @@ struct AQSAudioVoiceEngine : BaseAudioVoiceEngine std::unique_ptr newRealMIDIInOut(const char* name, ReceiveFunctor&& receiver) { + if (!m_midiClient) + return {}; + MIDIEndpointRef src = LookupMIDISource(name); if (!src) return {}; @@ -648,7 +669,8 @@ struct AQSAudioVoiceEngine : BaseAudioVoiceEngine ~AQSAudioVoiceEngine() { AudioQueueDispose(m_queue, false); - MIDIClientDispose(m_midiClient); + if (m_midiClient) + MIDIClientDispose(m_midiClient); } void pumpAndMixVoices()