Skip to content

Commit dfa3a78

Browse files
authored
Add security guidance for user-specified capacity in collections (#12415)
1 parent 659f623 commit dfa3a78

5 files changed

Lines changed: 60 additions & 5 deletions

File tree

xml/System.Collections.Generic/Dictionary`2.xml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,9 @@
458458
459459
460460
461+
> [!CAUTION]
462+
> 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.
463+
461464
## Examples
462465
The following code example creates a <xref:System.Collections.Generic.Dictionary%602> with a case-insensitive equality comparer for the current culture. The example adds four elements, some with lower-case keys and some with upper-case keys. The example then attempts to add an element with a key that differs from an existing key only by case, catches the resulting exception, and displays an error message. Finally, the example displays the elements in the dictionary.
463466
@@ -526,7 +529,8 @@
526529
527530
This constructor is an O(1) operation.
528531
529-
532+
> [!CAUTION]
533+
> 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.
530534
531535
## Examples
532536
The following code example creates a dictionary with an initial capacity of 4 and populates it with 4 entries.
@@ -1292,7 +1296,16 @@
12921296
<param name="capacity">The number of entries.</param>
12931297
<summary>Ensures that the dictionary can hold up to a specified number of entries without any further expansion of its backing storage.</summary>
12941298
<returns>The current capacity of the <see cref="T:System.Collections.Generic.Dictionary`2" />.</returns>
1295-
<remarks>To be added.</remarks>
1299+
<remarks>
1300+
<format type="text/markdown"><![CDATA[
1301+
1302+
## Remarks
1303+
1304+
> [!CAUTION]
1305+
> 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.
1306+
1307+
]]></format>
1308+
</remarks>
12961309
<exception cref="T:System.ArgumentOutOfRangeException">
12971310
<paramref name="capacity" /> is less than 0.</exception>
12981311
</Docs>

xml/System.Collections.Generic/HashSet`1.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ The following example demonstrates how to merge two disparate sets. This example
376376
## Remarks
377377
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`.
378378
379+
> [!CAUTION]
380+
> If `capacity` comes from user input, prefer using a constructor overload without a `capacity` parameter, and let 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.
381+
379382
]]></format>
380383
</remarks>
381384
</Docs>
@@ -506,6 +509,9 @@ The following example demonstrates how to merge two disparate sets. This example
506509
## Remarks
507510
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`.
508511
512+
> [!CAUTION]
513+
> If `capacity` comes from user input, prefer using a constructor overload without a `capacity` parameter, and let 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.
514+
509515
]]></format>
510516
</remarks>
511517
</Docs>
@@ -1230,7 +1236,16 @@ The following example demonstrates how to merge two disparate sets. This example
12301236
<param name="capacity">The minimum capacity to ensure.</param>
12311237
<summary>Ensures that this hash set can hold the specified number of elements without any further expansion of its backing storage.</summary>
12321238
<returns>The new capacity of this instance.</returns>
1233-
<remarks>To be added.</remarks>
1239+
<remarks>
1240+
<format type="text/markdown"><![CDATA[
1241+
1242+
## Remarks
1243+
1244+
> [!CAUTION]
1245+
> 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.
1246+
1247+
]]></format>
1248+
</remarks>
12341249
<exception cref="T:System.ArgumentOutOfRangeException">
12351250
<paramref name="capacity" /> is less than zero.</exception>
12361251
</Docs>

xml/System.Collections.Generic/List`1.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@
320320
321321
This constructor is an O(1) operation.
322322
323+
> [!CAUTION]
324+
> 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.
325+
323326
## Examples
324327
The following example demonstrates the <xref:System.Collections.Generic.List%601.%23ctor%28System.Int32%29> constructor. A <xref:System.Collections.Generic.List%601> of strings with a capacity of 4 is created, because the ultimate size of the list is known to be exactly 4. The list is populated with four strings, and a read-only copy is created by using the <xref:System.Collections.Generic.List%601.AsReadOnly%2A> method.
325328
@@ -1476,7 +1479,16 @@
14761479
<param name="capacity">The minimum capacity to ensure.</param>
14771480
<summary>Ensures that the capacity of this list is at least the specified <paramref name="capacity" />. If the current capacity is less than <paramref name="capacity" />, it is increased to at least the specified <paramref name="capacity" />.</summary>
14781481
<returns>The new capacity of this list.</returns>
1479-
<remarks>To be added.</remarks>
1482+
<remarks>
1483+
<format type="text/markdown"><![CDATA[
1484+
1485+
## Remarks
1486+
1487+
> [!CAUTION]
1488+
> 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.
1489+
1490+
]]></format>
1491+
</remarks>
14801492
</Docs>
14811493
</Member>
14821494
<Member MemberName="Exists">

xml/System.Collections.Generic/Queue`1.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@
335335
336336
This constructor is an O(`n`) operation, where `n` is `capacity`.
337337
338+
> [!CAUTION]
339+
> 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.
340+
338341
]]></format>
339342
</remarks>
340343
<exception cref="T:System.ArgumentOutOfRangeException">
@@ -842,7 +845,16 @@
842845
<param name="capacity">The minimum capacity to ensure.</param>
843846
<summary>Ensures that the capacity of this queue is at least the specified <paramref name="capacity" />. If the current capacity is less than <paramref name="capacity" />, it is increased to at least the specified <paramref name="capacity" />.</summary>
844847
<returns>The new capacity of this queue.</returns>
845-
<remarks>To be added.</remarks>
848+
<remarks>
849+
<format type="text/markdown"><![CDATA[
850+
851+
## Remarks
852+
853+
> [!CAUTION]
854+
> 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.
855+
856+
]]></format>
857+
</remarks>
846858
</Docs>
847859
</Member>
848860
<Member MemberName="GetEnumerator">

xml/System.Collections/ArrayList.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@
354354
355355
This constructor is an `O(n)` operation, where `n` is `capacity`.
356356
357+
> [!CAUTION]
358+
> 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.
359+
357360
]]></format>
358361
</remarks>
359362
<exception cref="T:System.ArgumentOutOfRangeException">

0 commit comments

Comments
 (0)