Copy error message to RtMidiWrapper before it's freed
#358
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.