Skip to content

midi_stream_decoder: copy an unterminated SysEx from its own F0; ignore empty buffers - #270

Open
nmplcpimenta wants to merge 1 commit into
celtera:masterfrom
nmplcpimenta:fix-sysex-offset
Open

nmplcpimenta wants to merge 1 commit into
celtera:masterfrom
nmplcpimenta:fix-sysex-offset

Conversation

@nmplcpimenta

@nmplcpimenta nmplcpimenta commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Problem

There are two bugs in midi1::input_state_machine::on_bytes_multi_segmented (include/libremidi/detail/midi_stream_decoder.hpp):

  1. Wrong copy offset. When a buffer contains other messages before an F0 that is not terminated in that buffer, the partial SysEx is copied with message.assign(bytes.begin(), bytes.begin() + size), which starts at the beginning of the buffer instead of at the F0. For example, 90 3C 7F F0 7E 01 followed by 09 01 F7 gives a note on and then the "SysEx" 90 3C 7F 09 01 F7.
  2. Empty buffer. An empty buffer reaches has_finished_sysex, which calls front() and back() on an empty span. This is undefined behaviour, and it trips the libc++ hardening assertion span<T>::front() on empty span. on_bytes_segmented already returns early on an empty buffer.

Change

  • The partial SysEx is copied from bytes.begin() + i_byte.
  • on_bytes_multi_segmented now returns early on an empty buffer, as on_bytes_segmented already does.

Tests

There is a new case in tests/unit/midi_stream_decoder.cpp, "midi1: SysEx after other messages in one buffer":

  • the example above must yield a note on and F0 7E 01 09 01 F7;
  • empty buffers passed to on_bytes_multi and on_bytes produce no messages.

On the current master, the first check fails and the second aborts under libc++ hardening.

Relation to the SysEx PR

Related to #271, which rewrites this F0 branch and also never calls front() on an empty span, so it fixes both bugs on its own. The two touch the same lines; whichever lands second, I'll rebase it. If #271 lands first, this PR shrinks to its test, which passes there as is.

🤖 Generated with Claude Code

…re empty buffers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jcelerier

Copy link
Copy Markdown
Member

heya! thanks, can you rebase on new master? I merged your other patch + some additional sysex fixes found during review

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants