lovrMicrophoneGetData: fix potential buffer overrun
With the check for samples==0 being done BELOW the assert for offset+samples<soundData->samples,
setting samples to 0 and then having more samples available in the mic than present in
the created buffer would cause buffer overrun
@@ -536,6 +536,10 @@ SoundData* lovrMicrophoneGetData(Microphone* microphone, size_t samples, SoundDa
return NULL;
}
+ if (samples == 0 || samples > availableSamples) {
+ samples = availableSamples;
+ }
+
if (soundData == NULL) {
soundData = lovrSoundDataCreate(samples, microphone->sampleRate, microphone->bitDepth, microphone->channelCount);
} else {
@@ -545,10 +549,6 @@ SoundData* lovrMicrophoneGetData(Microphone* microphone, size_t samples, SoundDa
lovrAssert(offset + samples <= soundData->samples, "Tried to write samples past the end of a SoundData buffer");
}
- if (samples == 0 || samples > availableSamples) {
- samples = availableSamples;
- }
-
uint8_t* data = (uint8_t*) soundData->blob->data + offset * (microphone->bitDepth / 8) * microphone->channelCount;
alcCaptureSamples(microphone->device, data, (ALCsizei) samples);
return soundData;
