Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions Rules/AvoidValidateRangeTypeMismatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
#endif
public sealed class AvoidValidateRangeTypeMismatch : IScriptRule
{
private static readonly HashSet<string> FloatingPointAndDecimalTypes = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
private static readonly HashSet<Type> FloatingPointAndDecimalTypes = new HashSet<Type>
{
"double", "System.Double",
"single", "float", "System.Single",
"decimal", "System.Decimal"
typeof(double),
typeof(float),
typeof(decimal)
};

private static readonly HashSet<string> IntegralTypes = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
private static readonly HashSet<Type> IntegralTypes = new HashSet<Type>
{
"byte", "System.Byte",
"sbyte", "System.SByte",
"short", "System.Int16",
"ushort", "System.UInt16",
"int", "System.Int32",
"uint", "System.UInt32",
"long", "System.Int64",
"ulong", "System.UInt64"
typeof(byte),
typeof(sbyte),
typeof(short),
typeof(ushort),
typeof(int),
typeof(uint),
typeof(long),
typeof(ulong)
};

public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
Expand All @@ -51,7 +51,23 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
foreach (ParameterAst parameterAst in parameterAsts)
{
TypeConstraintAst typeConstraint = parameterAst.Attributes.OfType<TypeConstraintAst>().FirstOrDefault();
if (typeConstraint == null || !FloatingPointAndDecimalTypes.Contains(typeConstraint.TypeName.FullName))
if (typeConstraint == null)
{
continue;
}

Type parameterType = typeConstraint.TypeName.GetReflectionType();
if (parameterType == null)
{
continue;
}

if (parameterType.IsArray)
{
parameterType = parameterType.GetElementType();
}

if (!FloatingPointAndDecimalTypes.Contains(parameterType))
{
continue;
}
Expand All @@ -60,7 +76,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (!IsValidateRangeAttribute(attributeAst)
|| attributeAst.PositionalArguments.Count < 2
|| !attributeAst.PositionalArguments.Take(2).Any(IsIntegralBoundary))
|| !attributeAst.PositionalArguments.Take(2).All(IsIntegralBoundary))
{
continue;
}
Expand All @@ -79,14 +95,12 @@ private static bool IsIntegralBoundary(ExpressionAst expressionAst)
{
if (expressionAst is ConstantExpressionAst constantExpressionAst)
{
return constantExpressionAst.Value != null
&& IntegralTypes.Contains(constantExpressionAst.Value.GetType().FullName);
return constantExpressionAst.Value != null && IntegralTypes.Contains(constantExpressionAst.Value.GetType());
}

if (expressionAst is ConvertExpressionAst convertExpressionAst)
{
return IntegralTypes.Contains(convertExpressionAst.Type.TypeName.FullName)
&& IsIntegralBoundary(convertExpressionAst.Child);
return IntegralTypes.Contains(convertExpressionAst.Type.TypeName.GetReflectionType());
}

if (expressionAst is UnaryExpressionAst unaryExpressionAst
Expand Down
6 changes: 5 additions & 1 deletion Tests/Rules/AvoidValidateRangeTypeMismatch.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ Describe 'AvoidValidateRangeTypeMismatch' {
$scripts = @(
'function Test { param([double][ValidateRange(1, 2)]$Value) }'
'function Test { param([single][ValidateRange(-1, 2)]$Value) }'
'function Test { param([decimal][ValidateRange(1, 2.0)]$Value) }'
'function Test { param([decimal][ValidateRange(1, 2)]$Value) }'
'function Test { param([double[]][ValidateRange(1, 2)]$Values) }'
'function Test { param([double][System.Management.Automation.ValidateRangeAttribute(1, 2)]$Value) }'
'function Test { param([double][ValidateRange([int]0.0, [int]100.0)]$Value) }'
'function Test { param([double][ValidateRange([int32]0.0, [int32]100.0)]$Value) }'
)

foreach ($scriptDefinition in $scripts) {
Expand All @@ -26,6 +29,7 @@ Describe 'AvoidValidateRangeTypeMismatch' {
'function Test { param([double][ValidateRange(1.0, 2.0)]$Value) }'
'function Test { param([single][ValidateRange(1.0, 2.0)]$Value) }'
'function Test { param([decimal][ValidateRange(1.0, 2.0)]$Value) }'
'function Test { param([double][ValidateRange(1, 2.0)]$Value) }'
'function Test { param([int][ValidateRange(1, 2)]$Value) }'
)

Expand Down
Loading