Skip to content

Commit

Permalink
Pa_Initalze(): Guard against recursive calls.
Browse files Browse the repository at this point in the history
This fixes a stack overflow when a diver uses portaudio as well like FlexAsio
  • Loading branch information
daschuer committed Mar 15, 2023
1 parent 68e963a commit 92625ce
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/common/pa_front.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#include <string.h>
#include <stdlib.h> /* needed for strtol() */
#include <assert.h> /* needed by PA_VALIDATE_ENDIANNESS */
#include <stdbool.h>

#include "portaudio.h"
#include "pa_util.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -371,6 +384,8 @@ PaError Pa_Initialize( void )
result = InitializeHostApis();
if( result == paNoError )
++initializationCount_;

initializing_ = false;
}

PA_LOGAPI_EXIT_PAERROR( "Pa_Initialize", result );
Expand Down

0 comments on commit 92625ce

Please sign in to comment.