SDK: VRCPortMidi - Index outside of bounds error
tracked
Mіcca
VRCPortMidi.cs, part of the VRChat SDK, has no bounds check for input data. This leads to log spam and data loss with MIDI-intensive inputs in both editor and client.
Error in editor:
IndexOutOfRangeException: Index was outside the bounds of the array.
VRC.SDKBase.Midi.VRCPortMidiInput.Update () (at ./Packages/com.vrchat.worlds/Runtime/VRCSDK/SDK3/Midi/VRCPortMidi.cs:71)
VRC.SDK3.Midi.VRCMidiHandler.Update () (at <947e39e17d4040a8ae3f6b8caf682c06>:0)
Error in client log:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at ÏÎÌÌÏÏÎÌÌÎÌÏÌÌÎÍÍÌÌÍÌÏÎ.Update () [0x00000] in <00000000000000000000000000000000>:0
at VRC.SDK3.Midi.VRCMidiHandler.Update () [0x00000] in <00000000000000000000000000000000>:0
2025.07.26 19:09:01 Error - Index was outside the bounds of the array.
The error occurs on VRCPortMidi.cs Line 71:
ConvertAndSend(_data[i], _data[i + 1], _data[i + 2]);
Proposed fix: Add a bounds check before entering the loop on line 69, such as:
count = count > _data.Length - 8 ? _data.Length - 8 : count;
Log In
StormRel
tracked
Mіcca
Update: After some digging, the correct fix appears to be changing VRCPortMidi.cs Line 68 to:
int count = (_input.Read(_data, 0, _data.Length / 8)) * 2;
Read() grabs a number of PmEvent structs, which are 8 bytes long; the Length parameter is for the number of PmEvents, NOT bytes. This appears to be a bit of an oversight of the C# adapter for PortMidi. This code change fixes the buffer issues.
There seems to be other issues with portmidi and high midi traffic, but this change at least fixes the exception and editor crash.