mackron / miniaudio

Audio playback and capture library written in C, in a single source file.
https://miniaud.io
Other
4.07k stars 361 forks source link

Fix UB when there are no audio devices #531

Closed Darkyenus closed 2 years ago

Darkyenus commented 2 years ago

When there are no capture nor playback devices, pContext->pDeviceInfos is NULL and pContext->playbackDeviceInfoCount is 0. Unfortunately, any arithmetic on NULL is UB, including trivial +0, which triggers UB sanitizer. This can lead to crashes, for example when compiling with Zig, which enables UBsan by default.

Note: this could easily be a oneliner, but the line was already long enough. I have also wanted to include an explicit comment, because without knowing that NULL+0 is UB, the code would seem insane.

Some extra context: SO about NULL arithmetic and Zig issue about the same problem elsewhere.

mackron commented 2 years ago

This is reasonable. Merged. Thanks!