Off-by-one error in juce_linux_Midi.cpp

Hi,

in the method createPort (const String& name, bool forInput, bool enableSubscription),
the idea is that the new port is inserted at index “port->getPortId()” of the ports array.

This is done with this line:

        ports.set (port->getPortId(), port);

The first port created has a portId == 1, the second is 2, etc…
BUT OwnedArray::set() has the following behaviour (doc excerpt):

If the index is less than zero, this method does nothing. If the index is beyond the end of the array, the new object is added to the end of the array.

So in our case, the first port has a portId of 1 and the ports array is empty. So ports.set(1, …) will write at index 0 of the array instead of index 1.

This is causing issues when deletePort calls ports.set (port->getPortId(), nullptr); : it is either adding a new nullptr at the end of the array, or it is deleting the wrong port…

Fix: add a while (ports.size() < port->getPortId()) ports.set(ports.size(), nullptr); just before the ports.set() call in createPort.

Thanks for reporting, there’s a fix on develop:

2 Likes