audio: fix device name use-after-free in AudioDevice::open #1012
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.
Fixes a use-after-free in
AudioDevice::open
, which occurs whenselecting a particular device by name (as opposed to using a default
device).
Specifically, when extracting a C-style string pointer from a
Option<CString>
, the option was consumed, its contents turned into apointer, and the original
CString
dropped. After that the C-stylestring pointer is dangling, as its backing rust CString has been freed.
To fix this, the CString must be kept alive, which is achieved by simply
creating an intermediary
Option<&CString>
, and consuming that.Example
When trying to open an audio recording device called
HD Webcam C615 Analog Mono
on my machine:device_ptr
points to@\xf4~UUU\x00\x00m C615 Analog Mono
(which when interpreted as a null-terminated C string is actually just@\xf4~UUU
), which shows that the user-provided name was clobbered somehow.After applying the proposed change, valgrind is appeased! 🌺