Fix UB when there are no audio devices

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.
This commit is contained in:
Jan Polák
2022-09-02 12:26:06 +02:00
committed by David Reid
parent 0c92dac883
commit be32dc0e04
+6 -1
View File
@@ -39913,7 +39913,12 @@ MA_API ma_result ma_context_get_devices(ma_context* pContext, ma_device_info** p
/* Capture devices. */
if (ppCaptureDeviceInfos != NULL) {
*ppCaptureDeviceInfos = pContext->pDeviceInfos + pContext->playbackDeviceInfoCount; /* Capture devices come after playback devices. */
*ppCaptureDeviceInfos = pContext->pDeviceInfos;
/* Capture devices come after playback devices. */
if (pContext->playbackDeviceInfoCount > 0) {
/* Conditional, because NULL+0 is undefined behavior. */
*ppCaptureDeviceInfos += pContext->playbackDeviceInfoCount;
}
}
if (pCaptureDeviceCount != NULL) {
*pCaptureDeviceCount = pContext->captureDeviceInfoCount;