From 5dc3f3626d63361f4cefc829bce9aedb3c5774cf Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 20 Dec 2013 00:47:12 -0800 Subject: [PATCH] Better protect against improper alSoundfontSamplesSOFT calls --- Alc/midi/base.c | 1 + OpenAL32/Include/alMidi.h | 1 + OpenAL32/alSoundfont.c | 29 ++++++++++++++++++++--------- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Alc/midi/base.c b/Alc/midi/base.c index f39aaba3..a5c50504 100644 --- a/Alc/midi/base.c +++ b/Alc/midi/base.c @@ -429,6 +429,7 @@ void ALsoundfont_Construct(ALsoundfont *self) self->Samples = NULL; self->NumSamples = 0; + RWLockInit(&self->Lock); self->Mapped = AL_FALSE; self->id = 0; diff --git a/OpenAL32/Include/alMidi.h b/OpenAL32/Include/alMidi.h index 70a2120d..fffb1174 100644 --- a/OpenAL32/Include/alMidi.h +++ b/OpenAL32/Include/alMidi.h @@ -117,6 +117,7 @@ typedef struct ALsoundfont { ALshort *Samples; ALint NumSamples; + RWLock Lock; volatile ALenum Mapped; ALuint id; diff --git a/OpenAL32/alSoundfont.c b/OpenAL32/alSoundfont.c index 7b9d7d0a..33b3ce3e 100644 --- a/OpenAL32/alSoundfont.c +++ b/OpenAL32/alSoundfont.c @@ -124,6 +124,7 @@ AL_API ALvoid AL_APIENTRY alSoundfontSamplesSOFT(ALuint sfid, ALenum type, ALsiz ALCdevice *device; ALCcontext *context; ALsoundfont *sfont; + void *ptr; context = GetContextRef(); if(!context) return; @@ -136,16 +137,21 @@ AL_API ALvoid AL_APIENTRY alSoundfontSamplesSOFT(ALuint sfid, ALenum type, ALsiz if(count <= 0) SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done); - // TODO: Lock and check ref count + WriteLock(&sfont->Lock); + if(sfont->ref != 0) + alSetError(context, AL_INVALID_OPERATION); + else if(sfont->Mapped) + alSetError(context, AL_INVALID_OPERATION); + else if(!(ptr=realloc(sfont->Samples, count * sizeof(ALshort)))) + alSetError(context, AL_OUT_OF_MEMORY); + else { - void *temp = realloc(sfont->Samples, count * sizeof(ALshort)); - if(!temp) - SET_ERROR_AND_GOTO(context, AL_OUT_OF_MEMORY, done); - sfont->Samples = temp; + sfont->Samples = ptr; sfont->NumSamples = count; - if(samples) + if(samples != NULL) memcpy(sfont->Samples, samples, count * sizeof(ALshort)); } + WriteUnlock(&sfont->Lock); done: ALCcontext_DecRef(context); @@ -168,10 +174,15 @@ AL_API ALvoid* AL_APIENTRY alSoundfontMapSamplesSOFT(ALuint sfid, ALsizei offset SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done); if(length <= 0 || (ALuint)length > (sfont->NumSamples*sizeof(ALshort) - offset)) SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done); - if(ExchangeInt(&sfont->Mapped, AL_TRUE) == AL_TRUE) - SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done); - ptr = (ALbyte*)sfont->Samples + offset; + ReadLock(&sfont->Lock); + if(sfont->ref != 0) + alSetError(context, AL_INVALID_OPERATION); + else if(ExchangeInt(&sfont->Mapped, AL_TRUE) == AL_TRUE) + alSetError(context, AL_INVALID_OPERATION); + else + ptr = (ALbyte*)sfont->Samples + offset; + ReadUnlock(&sfont->Lock); done: ALCcontext_DecRef(context); -- 2.11.4.GIT