Skip to content

Commit

Permalink
Fix runtime buffer overflow on MSVC (#336)
Browse files Browse the repository at this point in the history
From the MS docs about _alloca:
```
_alloca allocates size bytes from the program stack. The allocated space is automatically freed when the calling function exits (not when the allocation merely passes out of scope). 
```
So, using _alloca in a loop is a very bad idea, and this PR fixes this.
  • Loading branch information
Rossmaxx committed Jan 22, 2024
1 parent bfbd4c4 commit 04c6ad9
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/modules_limit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ uint32_t limiter_audio_module::process(uint32_t offset, uint32_t numsamples, uin
} else {
asc_led -= std::min(asc_led, numsamples);

// allocate fickdich on the stack before entering loop
STACKALLOC(float, fickdich, limiter.overall_buffer_size);
while(offset < numsamples) {
// cycle through samples
float inL = ins[0][offset];
Expand All @@ -135,7 +137,6 @@ uint32_t limiter_audio_module::process(uint32_t offset, uint32_t numsamples, uin
float tmpR;

// process gain reduction
STACKALLOC(float, fickdich, limiter.overall_buffer_size);
for (int i = 0; i < *params[param_oversampling]; i ++) {
tmpL = samplesL[i];
tmpR = samplesR[i];
Expand Down Expand Up @@ -356,6 +357,8 @@ uint32_t multibandlimiter_audio_module::process(uint32_t offset, uint32_t numsam
} else {
// process all strips
asc_led -= std::min(asc_led, numsamples);
// allocate fickdich on the stack before entering loop to avoid buffer overflow
STACKALLOC(float, fickdich, broadband.overall_buffer_size);
while(offset < numsamples) {
float inL = 0.f; // input
float inR = 0.f;
Expand Down Expand Up @@ -465,7 +468,6 @@ uint32_t multibandlimiter_audio_module::process(uint32_t offset, uint32_t numsam
}

// process broadband limiter
STACKALLOC(float, fickdich, broadband.overall_buffer_size);
tmpL = resL[o];
tmpR = resR[o];
broadband.process(tmpL, tmpR, fickdich);
Expand Down Expand Up @@ -754,6 +756,8 @@ uint32_t sidechainlimiter_audio_module::process(uint32_t offset, uint32_t numsam
} else {
// process all strips
asc_led -= std::min(asc_led, numsamples);
// allocate fickdich on the stack before loops to avoid buffer overflow
STACKALLOC(float, fickdich, broadband.overall_buffer_size);
while(offset < numsamples) {
float inL = 0.f; // input
float inR = 0.f;
Expand Down Expand Up @@ -850,7 +854,6 @@ uint32_t sidechainlimiter_audio_module::process(uint32_t offset, uint32_t numsam
}

// process broadband limiter
STACKALLOC(float, fickdich, broadband.overall_buffer_size);
tmpL = resL[o];
tmpR = resR[o];
broadband.process(tmpL, tmpR, fickdich);
Expand Down

0 comments on commit 04c6ad9

Please sign in to comment.