From be32dc0e046ba1ada831a0f006ed8a5c89fd9fc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Pol=C3=A1k?= <456647@muni.cz> Date: Fri, 2 Sep 2022 12:26:06 +0200 Subject: [PATCH] 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. --- miniaudio.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/miniaudio.h b/miniaudio.h index a05b086c..c4b156a7 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -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;