We currently suppress warnings of CA2014 in parts of the build. stackallocs in loops can cause stack overflows, so we need to refactor our code to fix this.
See:
Suppression added in https://github.com/PowerShell/PowerShell/pull/13530
Only first case has real cycle.
We should work to factor out the loops that are not loops in these cases as well. For example, the do { } while (false); examples sorely need to be turned into methods
Both first and second are real loops, no?
For 1 case. StackAlloc is used only (1) ~for large output~, (2) if new line is needed, (3) once on last iteration - so we could add comment that it is safe to suppress.
For 2 case. We can move StackAlloc out the cycle.
For 3 case. We can remove the cycle and replace break-s with return-s.
It strikes me that in case 1 we just need a buffer -- we could allocate outside of the loop and simply reuse the buffer space within the loop. I imagine that's the intent of the warning anyway.
We could put all after line 2570 after the while cycle - it output a tail once.
Most helpful comment
For 1 case. StackAlloc is used only (1) ~for large output~, (2) if new line is needed, (3) once on last iteration - so we could add comment that it is safe to suppress.
For 2 case. We can move StackAlloc out the cycle.
For 3 case. We can remove the cycle and replace break-s with return-s.