From 32a745d9f9e10b71378b472b99c282ae9584baec Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 14 May 2018 08:56:48 +0100 Subject: [PATCH] Avoided a possible null pointer when opening non-functional linux midi devices --- .../native/juce_linux_Midi.cpp | 52 +++++++++---------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/modules/juce_audio_devices/native/juce_linux_Midi.cpp b/modules/juce_audio_devices/native/juce_linux_Midi.cpp index 289bb414da..4d2a6e90c0 100644 --- a/modules/juce_audio_devices/native/juce_linux_Midi.cpp +++ b/modules/juce_audio_devices/native/juce_linux_Midi.cpp @@ -113,9 +113,9 @@ public: snd_seq_ev_clear (&event); auto numBytes = (long) message.getRawDataSize(); - const uint8* data = message.getRawData(); + auto* data = message.getRawData(); - auto* seqHandle = client.get(); + auto seqHandle = client.get(); bool success = true; while (numBytes > 0) @@ -154,12 +154,12 @@ public: void createPort (const String& name, bool enableSubscription) { - if (auto* seqHandle = client.get()) + if (auto seqHandle = client.get()) { const unsigned int caps = - isInput - ? (SND_SEQ_PORT_CAP_WRITE | (enableSubscription ? SND_SEQ_PORT_CAP_SUBS_WRITE : 0)) - : (SND_SEQ_PORT_CAP_WRITE | (enableSubscription ? SND_SEQ_PORT_CAP_SUBS_READ : 0)); + isInput ? (SND_SEQ_PORT_CAP_WRITE | (enableSubscription ? SND_SEQ_PORT_CAP_SUBS_WRITE : 0)) + : (SND_SEQ_PORT_CAP_WRITE | (enableSubscription ? SND_SEQ_PORT_CAP_SUBS_READ : 0)); + portId = snd_seq_create_simple_port (seqHandle, name.toUTF8(), caps, SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION); @@ -260,12 +260,16 @@ private: jassert (instance == nullptr); snd_seq_open (&handle, "default", SND_SEQ_OPEN_DUPLEX, 0); - snd_seq_nonblock (handle, SND_SEQ_NONBLOCK); - snd_seq_set_client_name (handle, JUCE_ALSA_MIDI_NAME); - clientId = snd_seq_client_id(handle); - // It's good idea to pre-allocate a good number of elements - ports.ensureStorageAllocated (32); + if (handle != nullptr) + { + snd_seq_nonblock (handle, SND_SEQ_NONBLOCK); + snd_seq_set_client_name (handle, JUCE_ALSA_MIDI_NAME); + clientId = snd_seq_client_id (handle); + + // It's good idea to pre-allocate a good number of elements + ports.ensureStorageAllocated (32); + } } ~AlsaClient() @@ -295,9 +299,10 @@ private: void run() override { + auto seqHandle = client.get(); + const int maxEventSize = 16 * 1024; snd_midi_event_t* midiParser; - snd_seq_t* seqHandle = client.get(); if (snd_midi_event_new (maxEventSize, &midiParser) >= 0) { @@ -321,8 +326,8 @@ private: if (snd_seq_event_input (seqHandle, &inputEvent) >= 0) { // xxx what about SYSEXes that are too big for the buffer? - const long numBytes = snd_midi_event_decode (midiParser, buffer, - maxEventSize, inputEvent); + auto numBytes = snd_midi_event_decode (midiParser, buffer, + maxEventSize, inputEvent); snd_midi_event_reset_decode (midiParser); @@ -360,7 +365,7 @@ static AlsaClient::Port* iterateMidiClient (const AlsaClient::Ptr& client, { AlsaClient::Port* port = nullptr; - snd_seq_t* seqHandle = client->get(); + auto seqHandle = client->get(); snd_seq_port_info_t* portInfo = nullptr; snd_seq_port_info_alloca (&portInfo); @@ -406,7 +411,7 @@ static AlsaClient::Port* iterateMidiDevices (bool forInput, AlsaClient::Port* port = nullptr; auto client = AlsaClient::getInstance(); - if (auto* seqHandle = client->get()) + if (auto seqHandle = client->get()) { snd_seq_system_info_t* systemInfo = nullptr; snd_seq_client_info_t* clientInfo = nullptr; @@ -503,8 +508,7 @@ void MidiOutput::sendMessageNow (const MidiMessage& message) } //============================================================================== -MidiInput::MidiInput (const String& nm) - : name (nm), internal (nullptr) +MidiInput::MidiInput (const String& nm) : name (nm) { } @@ -538,8 +542,6 @@ StringArray MidiInput::getDevices() MidiInput* MidiInput::openDevice (int deviceIndex, MidiInputCallback* callback) { - MidiInput* newDevice = nullptr; - StringArray devices; auto* port = iterateMidiDevices (true, devices, deviceIndex); @@ -548,26 +550,22 @@ MidiInput* MidiInput::openDevice (int deviceIndex, MidiInputCallback* callback) jassert (port->isValid()); - newDevice = new MidiInput (devices [deviceIndex]); + auto newDevice = new MidiInput (devices [deviceIndex]); port->setupInput (newDevice, callback); newDevice->internal = port; - return newDevice; } MidiInput* MidiInput::createNewDevice (const String& deviceName, MidiInputCallback* callback) { - MidiInput* newDevice = nullptr; - auto client = AlsaClient::getInstance(); auto* port = client->createPort (deviceName, true, true); jassert (port->isValid()); - newDevice = new MidiInput (deviceName); + auto newDevice = new MidiInput (deviceName); port->setupInput (newDevice, callback); newDevice->internal = port; - return newDevice; } @@ -584,7 +582,7 @@ MidiOutput* MidiOutput::createNewDevice (const String&) { return nul MidiOutput::~MidiOutput() {} void MidiOutput::sendMessageNow (const MidiMessage&) {} -MidiInput::MidiInput (const String& nm) : name (nm), internal (nullptr) {} +MidiInput::MidiInput (const String& nm) : name (nm) {} MidiInput::~MidiInput() {} void MidiInput::start() {} void MidiInput::stop() {}