Fix CA2014: Stackalloc in loops
See original GitHub issueWe 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
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
CA2014: Do not use stackalloc in loops (code analysis)
If stackalloc is used in a loop, it can lead to stack overflows due to exhausting the stack memory. How to fix violations....
Read more >Breaking change: CA2014: Do not use stackalloc in loops
Rule CA2014 looks for C# code where a stackalloc expression is used inside a loop. stackalloc allocates memory from the current stack frame....
Read more >How do I find where I am using stackalloc in loops? ...
I just opened another of my projects which looks more like yours. When I tried to investigate, I found the possible confusion: The...
Read more >Dos and Don'ts of stackalloc - Random Thoughts
DON'T: Use stackalloc in non-constant loops. Even if you allocate a fixed length amount of data on the stack, doing so in a...
Read more >Automatically Identify Bugs in Your Code With .NET 5
The fix is to add AsSpan calls in this case: ... CA2014 — Do not use “stackalloc” in loops ... Warning CA2014 Potential...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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.
Both first and second are real loops, no?