Throw when too many parameter sets are defined (#17881)

This commit is contained in:
Frode Flaten
2022-08-22 12:11:17 -07:00
committed by GitHub
parent 27cc09e1b2
commit a15bfcb501
2 changed files with 21 additions and 1 deletions
@@ -163,6 +163,13 @@ namespace System.Management.Automation
/// </summary>
private uint _nextAvailableParameterSetIndex;
/// <summary>
/// The maximum number of parameter sets allowed. Limit is set by the use
/// of a uint bitmask to store which parameter sets a parameter is included in.
/// See <see cref="ParameterSetSpecificMetadata.ParameterSetFlag"/>.
/// </summary>
private const uint MaxParameterSetCount = 32;
/// <summary>
/// Gets the number of parameter sets that were declared for the command.
/// </summary>
@@ -228,7 +235,7 @@ namespace System.Management.Automation
// A parameter set name should only be added once
if (index == -1)
{
if (_nextAvailableParameterSetIndex == uint.MaxValue)
if (_nextAvailableParameterSetIndex >= MaxParameterSetCount)
{
// Don't let the parameter set index overflow
ParsingMetadataException parsingException =
@@ -170,6 +170,19 @@ Describe "Tests for parameter binding" -Tags "CI" {
( get-foo -b b a c d ) -join ',' | Should -BeExactly 'a,c,d'
}
It 'Too many parameter sets defined' {
$scriptblock = {
param($numSets=1)
$parameters = (1..($numSets) | ForEach-Object { "[Parameter(parametersetname='set$_')]`$a$_" }) -join ', '
$body = "param($parameters) 'working'"
$sb = [scriptblock]::Create($body)
& $sb -a1 123
}
& $scriptblock -numSets 32 | Should -Be 'working'
{ & $scriptblock -numSets 33 } | Should -Throw -ErrorId 'ParsingTooManyParameterSets'
}
It 'Default parameter set with value from remaining arguments case 1' {
function get-foo
{