Skip to content

MIDI device change causes crash in clap-host #63

Description

@AleksandrMol

MIDI device change causes crash in clap-host

OS: Fedora KDE
MIDI device: Yamaha PSR-R300

Steps to reproduce

  1. Launch clap-host.
  2. Go to File → Settings.
  3. Under MIDI → Device, switch the device to USB MIDI Interface.
  4. Click OK.

The host aborts with SIGABRT.

MIDI settings window

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.

Code with the proposed fix

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions