# Integer overflow in AudioBlock internal functions

**URL:** <https://forum.juce.com/t/integer-overflow-in-audioblock-internal-functions/49106>\
**Category:** General JUCE discussion\
**Created:** [December 7, 2021, 11:18am UTC](https://forum.juce.com/t/integer-overflow-in-audioblock-internal-functions/49106 "2021-12-07T11:18:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [December 7, 2021, 11:18am UTC](https://forum.juce.com/t/integer-overflow-in-audioblock-internal-functions/49106/1 "2021-12-07T11:18:16Z")

</div>

I just ran into a crash caused by an integer overflow when clearing a huge `AudioBlock` of `SIMDRegister<float>` samples:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/1/1163f251c001e363ce1d56c2bf0d98affa537d84.png)

As you can see in the debugger screenshot above, the int cast results in a negative number. I see that the int cast is necessary to interface with `FloatVectorOperations::clear` – which in turn internally casts the values back to size\_t 🙄

Would be great if that could be fixed somehow, not sure if e.g. templating the size type of all `FloatVectorOperations` would be an option? For now, I just replaced the call to `FloatVectorOperations::clear` with a plain `zeromem` in our juce fork which resolves the issue for us but that doesn’t seem like a general solution and I just wait until we run into another case like that for another `AudioBlock` function.

---

<div class="post-metadata">

**Author:** ![JeffMcClintock](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jeffmcclintock/32/21430_2.png) [@JeffMcClintock](https://forum.juce.com/u/JeffMcClintock)\
**Post date:** [December 7, 2021, 8:32pm UTC](https://forum.juce.com/t/integer-overflow-in-audioblock-internal-functions/49106/2 "2021-12-07T20:32:06Z")

</div>

most of the C++ committee agrees that ‘size\_t’ being unsigned was a mistake, for this very reason (integer overflow). Unless you really expect a plugin to have more than 2,147,483,647 channels, just use ‘int’.

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [December 7, 2021, 9:24pm UTC](https://forum.juce.com/t/integer-overflow-in-audioblock-internal-functions/49106/3 "2021-12-07T21:24:21Z")

</div>

In fact, I think the committee advises to just use `int`, unless you have a really good reason not to! Unsigned simply has too many potential issues.

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [December 7, 2021, 10:43pm UTC](https://forum.juce.com/t/integer-overflow-in-audioblock-internal-functions/49106/4 "2021-12-07T22:43:44Z")

</div>

I think you both are missing the point here. This is not about unsigned vs signed integer. This is about casting a 64 bit `size_t` into a 32 bit `int`.

And no, of course I don’t expect my plugin too have more than 2,147,483,647 channels but as you can see in the debugger screenshot above our current plugin indeed needs a buffer capable of holding 1,888,608,256 `dsp::SIMDRegister<float>` samples in a buffer. For a `SIMDRegister<float>` the `sizeFactor` in the audio buffer is 4 so the multiplication result `numSamples * sizeFactor` in the `juce::dsp::AudioBlock` internals results in 7,554,433,024 samples in the buffer to be cleared. This value is then casted into a 32 bit `int` which results in a negative number that can be seen in the debugger screenshot which is passed to `FloatVectorOperations::clear`, resulting in a crash.

It seems that the maximum number of samples that can be handled by some `dsp::AudioBlock` member functions is limited by the maximum value of a 32 bit int, although the class uses 64 bit types for the sample count.

I would be totally fine with using signed 64 bit integers, this would definitively solve the problem, so again, this is not about signed vs unsigned but about casting 64 bit to 32 bit and this is about a real issue I ran into and not about some theoretical thing like

> [@JeffMcClintock](#):
>
> expect a plugin to have more than 2,147,483,647 channels

---

<div class="post-metadata">

**Author:** ![JeffMcClintock](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jeffmcclintock/32/21430_2.png) [@JeffMcClintock](https://forum.juce.com/u/JeffMcClintock)\
**Post date:** [December 8, 2021, 6:14pm UTC](https://forum.juce.com/t/integer-overflow-in-audioblock-internal-functions/49106/5 "2021-12-08T18:14:24Z")

</div>

Ah, you’re holding 7.5 _Billion_ samples in a buffer? That’s quite a lot 😉 (but not unheard of)

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [January 18, 2022, 2:21pm UTC](https://forum.juce.com/t/integer-overflow-in-audioblock-internal-functions/49106/6 "2022-01-18T14:21:45Z")

</div>

Now that the team obviously added `size_t` support to `FloatVectorOperations` (thanks for that!)

> <https://github.com/juce-framework/JUCE/commit/ca5c9fdae6319d159536250dcac5f92753edf8b3>

wouldn’t it be a good idea to remove the now not longer needed int casts in these internal functions to resolve the issues that I described here?

> [@JeffMcClintock](#):
>
> Ah, you’re holding 7.5 _Billion_ samples in a buffer? That’s quite a lot 😉 (but not unheard of)

Yes, that’s exactly what I do 😉

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [January 18, 2022, 3:33pm UTC](https://forum.juce.com/t/integer-overflow-in-audioblock-internal-functions/49106/7 "2022-01-18T15:33:41Z")

</div>

> [@PluginPenguin](#):
>
> wouldn’t it be a good idea to remove the now not longer needed int casts in these internal functions to resolve the issues that I described here?

I have that change on a branch, and it should be on develop in a few days.

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [January 18, 2022, 3:49pm UTC](https://forum.juce.com/t/integer-overflow-in-audioblock-internal-functions/49106/8 "2022-01-18T15:49:30Z")

</div>

Great news, thank you

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [January 28, 2022, 6:48pm UTC](https://forum.juce.com/t/integer-overflow-in-audioblock-internal-functions/49106/9 "2022-01-28T18:48:46Z")

</div>

Thanks for your patience, this change is now on develop:

> <https://github.com/juce-framework/JUCE/commit/7d21a967e4dffec6714affb888e68bddaefef934>

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [January 29, 2022, 9:21am UTC](https://forum.juce.com/t/integer-overflow-in-audioblock-internal-functions/49106/10 "2022-01-29T09:21:55Z")

</div>

Thank you, that should fix all the issues we were having, I’ll pull that changes next week.

One thing that came to my mind regarding the related `FloatVectorOperations` refactoring: While the implementation strategy with the `detail::NameForwarder` template seems quite clever, with that change all the functions disappeared from the Doxygen generated documentation. This is what the dev branch documentation looks like right now on the JUCE website:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/2/2e9b6d5ced0202f93383824ffab9677bc092ddf6.png)

Developers with some JUCE experience will know that the old functions are there, but newcomers might have a hard time finding them if they don’t find them by chance with their IDE’s autocompletion. I’m no Doxygen pro, but is there any trick to at least make the now undocumented functions re-appear there?

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [January 29, 2022, 2:58pm UTC](https://forum.juce.com/t/integer-overflow-in-audioblock-internal-functions/49106/11 "2022-01-29T14:58:40Z")

</div>

Thanks for pointing that out, I’ll try to think of a workaround.
