Copy error message to RtMidiWrapper before it's freed#358
Merged
garyscavone merged 1 commit intothestk:masterfrom Aug 25, 2025
Merged
Copy error message to RtMidiWrapper before it's freed#358garyscavone merged 1 commit intothestk:masterfrom
RtMidiWrapper before it's freed#358garyscavone merged 1 commit intothestk:masterfrom
Conversation
Contributor
Author
|
Here is a more minimal repro program in C, to rule out anything FFI-related: // clang -I./rtmidi test.c -o test -L./rtmidi/root/lib/ -lrtmidi && ./test
#include <stdio.h>
#include <rtmidi_c.h>
int main(int argc, char **argv) {
RtMidiOutPtr out = rtmidi_out_create(0, argv[0]);
rtmidi_open_port(out, 999, "invalid_port");
printf("ok=%d\nmsg=%s\n", out->ok, out->msg);
return 0;
}$ clang -I./rtmidi test.c -o test -L./rtmidi/root/lib/ -lrtmidi && ./test
MidiOutAlsa::openPort: the 'portNumber' argument (999) is invalid.
ok=0
msg=�xƁ�U |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In 4950a42 (2016) the C wrapper started catching exceptions and copying the error message to the wrapper struct. I'm not much of a C++ person, so I could be missing something here, but it seems the error string gets freed when the exception goes out of scope, leading to a use-after-free.
I noticed this while writing an FFI wrapper around RtMidi. Here is a minimal repro script.
The output looks like this:
I can see the string getting freed in the exception destructor, invoked at the end of
rtmidi_open_port:With the patch, it's fixed:
On the other hand, I can't believe I'm the first person to catch this since 2016, so apologies if this is something I'm doing wrong.