f (size_t n)
{
char str[n];
leads to a possible exploit where the input is manipulated so n is large, causing a DoS attack (at best) or full exploit at worse. I'm not saying that banning VLAs solves every problem though.However the main reason we forbid VLAs in all our code is because thread stacks (particularly on 32 bit or in kernel) are quite limited in depth and so you want to be careful with stack frame size. VLAs make it harder to compute and thus check stack frame sizes at compile time, making the -Wstack-usage warning less effective. Large arrays get allocated on the heap instead.
The same is true for most recursive calls, should recursion be also banned in programming languages?
None of this applies to VLA arguments.
DoS Risk? No one cares too much about that - the problem with VLAs is stack smashing, which then allows aribtrary user-supplied code to be executed.
You cannot do that with malloc() and friends.
But what if the length of your variable length array is, say, gigabytes, you've blown way past the guard pages, and your pointer is now in non-stack kernel land.
You'd have to check the stack pointer all the time to be sure, that's prohibitive performance-wise. Ironically, x86 kind of had that in hardware back when segmentation was still used.
https://msrc-blog.microsoft.com/2019/07/16/a-proactive-appro...
https://research.google/pubs/pub46800/
https://support.apple.com/guide/security/memory-safe-iboot-i...
Maybe you could give an helping hand to Microsoft, Apple and Google, they are in need of carefull C coders.
That means there are a lot of not careful enough developers (AKA, human ones) that will write a lot of C just because they need some change here or there.
1. The stack-smashing pattern is simple, straightforward and sure to be used often. Other ways to smash the stack require some more "effort"...
2. It's not just _you_ who can smash the stack. It's the fact that anyone who calls your function will smash the stack if they pass some large numeric value.
https://en.wikipedia.org/wiki/Stack_buffer_overflow
so, pretend I said "overflow" instead of "smash" in my post.
That's a mute point for C's target audience because they already understand that they need to be mindful of what the language does.
int A[100000000];
Also has no protection.