MIDI device change causes crash in clap-host
OS: Fedora KDE
MIDI device: Yamaha PSR-R300
Steps to reproduce
- Launch
clap-host.
- Go to File → Settings.
- Under MIDI → Device, switch the device to USB MIDI Interface.
- Click OK.
The host aborts with SIGABRT.
Possible cause
I traced the crash to Engine::audioCallback() in host/engine.cc.
The code checks whether the MIDI buffer is empty, but then unconditionally accesses midiBuf[1] and midiBuf[2].
My understanding is that RtMidiIn::getMessage() can return MIDI messages shorter than 3 bytes. In that case, accessing these indices can trigger the std::vector::operator[] bounds assertion and abort the process.
The relevant code is:
if (midiBuf.empty())
break;
uint8_t eventType = midiBuf[0] >> 4;
uint8_t channel = midiBuf[0] & 0xf;
uint8_t data1 = midiBuf[1];
uint8_t data2 = midiBuf[2];
Possible fix
I tested the following guard locally:
if (midiBuf.size() < 3)
continue;
placed before accessing midiBuf[1] and midiBuf[2].
With this change, the crash no longer occurs in my setup.
I would appreciate someone familiar with the project/RtMidi integration checking whether this is the appropriate way to handle shorter MIDI messages, or whether the MIDI input should be handled differently.
MIDI device change causes crash in
clap-hostOS: Fedora KDE
MIDI device: Yamaha PSR-R300
Steps to reproduce
clap-host.The host aborts with
SIGABRT.Possible cause
I traced the crash to
Engine::audioCallback()inhost/engine.cc.The code checks whether the MIDI buffer is empty, but then unconditionally accesses
midiBuf[1]andmidiBuf[2].My understanding is that
RtMidiIn::getMessage()can return MIDI messages shorter than 3 bytes. In that case, accessing these indices can trigger thestd::vector::operator[]bounds assertion and abort the process.The relevant code is:
Possible fix
I tested the following guard locally:
placed before accessing
midiBuf[1]andmidiBuf[2].With this change, the crash no longer occurs in my setup.
I would appreciate someone familiar with the project/RtMidi integration checking whether this is the appropriate way to handle shorter MIDI messages, or whether the MIDI input should be handled differently.