From 92625ce5f698d03cd5abff001e5db1b6b980ae7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Tue, 14 Mar 2023 23:33:10 +0100 Subject: [PATCH] Pa_Initalze(): Guard against recursive calls. This fixes a stack overflow when a diver uses portaudio as well like FlexAsio --- src/common/pa_front.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/common/pa_front.c b/src/common/pa_front.c index 89b4335b8..ebf1789dd 100644 --- a/src/common/pa_front.c +++ b/src/common/pa_front.c @@ -67,6 +67,7 @@ #include #include /* needed for strtol() */ #include /* needed by PA_VALIDATE_ENDIANNESS */ +#include #include "portaudio.h" #include "pa_util.h" @@ -154,6 +155,7 @@ static PaUtilHostApiRepresentation **hostApis_ = 0; static int hostApisCount_ = 0; static int defaultHostApiIndex_ = 0; static int initializationCount_ = 0; +static volatile bool initializing_ = false; static int deviceCount_ = 0; PaUtilStreamRepresentation *firstOpenStream_ = NULL; @@ -360,8 +362,19 @@ PaError Pa_Initialize( void ) ++initializationCount_; result = paNoError; } + else if( initializing_ ) + { + // a concurrent initialization is already running + result = paInternalError; + } else { + // set initializing_ here to + // let recursive calls execute the if branch above. + // This can happen if a driver like FlexAsio itself uses portaudio + // and avoids a stack overflow in the user application. + initializing_ = true; + PA_VALIDATE_TYPE_SIZES; PA_VALIDATE_ENDIANNESS; @@ -371,6 +384,8 @@ PaError Pa_Initialize( void ) result = InitializeHostApis(); if( result == paNoError ) ++initializationCount_; + + initializing_ = false; } PA_LOGAPI_EXIT_PAERROR( "Pa_Initialize", result );