Skip to content

Add safety warnings to stackalloc examples - #56358

Open
GrabYourPitchforks wants to merge 2 commits into
mainfrom
levib/stackalloc_fix_examples
Open

GrabYourPitchforks wants to merge 2 commits into
mainfrom
levib/stackalloc_fix_examples

Conversation

@GrabYourPitchforks

@GrabYourPitchforks GrabYourPitchforks commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

The prose and examples at https://learn.microsoft.com/dotnet/csharp/language-reference/operators/stackalloc don't sufficiently convey the hazard of allowing negative values to flow to the stackalloc operator.

This PR updates the examples to include the necessary safety check and updates the text to explain the hazard of process termination.


Internal previews

File Preview link
docs/csharp/language-reference/operators/snippets/shared/StackallocOperator.cs Learn preview
docs/csharp/language-reference/operators/stackalloc.md Learn preview

Build report

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The guidance incorrectly implies that the examples cap user input rather than only stack allocation size.

1 open finding
What changed in this PR

Adds safeguards that prevent negative lengths from reaching stackalloc and documents the process-termination risk.

Changes:

  • Adds nonnegative checks to two allocation examples.
  • Adds safety guidance for user-derived lengths.
File Description
stackalloc.md Documents negative-length risks.
StackallocOperator.cs Guards stack allocations against negative lengths.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread docs/csharp/language-reference/operators/stackalloc.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants