Add security guidance for user-specified capacity in collections#12415
Open
Add security guidance for user-specified capacity in collections#12415
Conversation
Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Update documentation for user specified capacity in collections
Add security guidance for user-specified capacity in collections
Mar 18, 2026
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-collections |
gewarren
reviewed
Mar 18, 2026
xml/System.Collections/ArrayList.xml
Outdated
| This constructor is an `O(n)` operation, where `n` is `capacity`. | ||
|
|
||
| > [!CAUTION] | ||
| > If `capacity` comes from user input, prefer using the parameterless constructor and letting the collection resize as elements are added. If you must use a user-specified value, either clamp it to a reasonable limit (for example, `Math.Clamp(untrustedValue, 0, 20)`) or verify that the element count matches the specified value. |
Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
gewarren
approved these changes
Mar 18, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Adds security-focused documentation guidance warning against passing untrusted user input as capacity to collection constructors or EnsureCapacity, helping reduce potential denial-of-service risks from excessive allocation.
Changes:
- Add
[!CAUTION]callouts tocapacity-accepting constructors for several collections. - Replace placeholder
To be added.remarks onEnsureCapacity(int)with a full markdown remarks section including the caution guidance. - Keep the guidance consistent across affected APIs (prefer natural growth, clamp, or validate claimed size).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| xml/System.Collections/ArrayList.xml | Adds CAUTION guidance to the ArrayList(int capacity) constructor remarks. |
| xml/System.Collections.Generic/Queue`1.xml | Adds CAUTION guidance to Queue(int capacity) and documents EnsureCapacity(int) remarks. |
| xml/System.Collections.Generic/List`1.xml | Adds CAUTION guidance to List(int capacity) and documents EnsureCapacity(int) remarks. |
| xml/System.Collections.Generic/HashSet`1.xml | Adds CAUTION guidance to HashSet(int capacity) and documents EnsureCapacity(int) remarks. |
| xml/System.Collections.Generic/Dictionary`2.xml | Adds CAUTION guidance to Dictionary(int capacity) and documents EnsureCapacity(int) remarks. |
You can also share your feedback on Copilot code review. Take the survey.
Comment on lines
+529
to
+530
| > [!CAUTION] | ||
| > If `capacity` comes from user input, prefer using a constructor without a capacity parameter and letting the collection resize as elements are added. If you must use a user-specified value, either clamp it to a reasonable limit (for example, `Math.Clamp(untrustedValue, 0, 20)`) or verify that the element count matches the specified value. |
| Since resizes are relatively expensive (require rehashing), this attempts to minimize the need to resize by setting the initial capacity based on the value of the `capacity`. | ||
|
|
||
| > [!CAUTION] | ||
| > If `capacity` comes from user input, prefer using the parameterless constructor and letting the collection resize as elements are added. If you must use a user-specified value, either clamp it to a reasonable limit (for example, `Math.Clamp(untrustedValue, 0, 20)`) or verify that the element count matches the specified value. |
|
|
||
|
|
||
| > [!CAUTION] | ||
| > If `capacity` comes from user input, prefer using a constructor without a capacity parameter and letting the collection resize as elements are added. If you must use a user-specified value, either clamp it to a reasonable limit (for example, `Math.Clamp(untrustedValue, 0, 20)`) or verify that the element count matches the specified value. |
| ## Remarks | ||
|
|
||
| > [!CAUTION] | ||
| > If `capacity` comes from user input, prefer letting the collection resize itself as elements are added instead of calling this method. If you must use a user-specified value, either clamp it to a reasonable limit (for example, `Math.Clamp(untrustedValue, 0, 20)`) or verify that the element count matches the specified value. |
| Since resizes are relatively expensive (require rehashing), this attempts to minimize the need to resize by setting the initial capacity based on the value of the `capacity`. | ||
|
|
||
| > [!CAUTION] | ||
| > If `capacity` comes from user input, prefer using the parameterless constructor and letting the collection resize as elements are added. If you must use a user-specified value, either clamp it to a reasonable limit (for example, `Math.Clamp(untrustedValue, 0, 20)`) or verify that the element count matches the specified value. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Passing untrusted user input directly as
capacityto collection constructors orEnsureCapacitycan enable denial-of-service via excessive memory allocation. Adds[!CAUTION]remarks to the affected APIs recommending safer patterns.Affected APIs
Constructors (
int capacityoverload):List<T>,Dictionary<TKey,TValue>,HashSet<T>,Queue<T>,ArrayListEnsureCapacity(int capacity)methods:List<T>,Dictionary<TKey,TValue>,HashSet<T>,Queue<T>Guidance added
Each caution note recommends:
EnsureCapacitymethods that previously hadTo be added.remarks now include a full## Remarkssection with the caution.Original prompt
capacityin collections #12272✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.