|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Globalization; |
| 7 | +using System.Linq; |
| 8 | +using System.Management.Automation.Language; |
| 9 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; |
| 10 | +#if !CORECLR |
| 11 | +using System.ComponentModel.Composition; |
| 12 | +#endif |
| 13 | + |
| 14 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// AvoidValidateRangeTypeMismatch: Detect integer ValidateRange bounds on floating-point and decimal parameters. |
| 18 | + /// </summary> |
| 19 | +#if !CORECLR |
| 20 | + [Export(typeof(IScriptRule))] |
| 21 | +#endif |
| 22 | + public sealed class AvoidValidateRangeTypeMismatch : IScriptRule |
| 23 | + { |
| 24 | + private static readonly HashSet<string> FloatingPointAndDecimalTypes = new HashSet<string>(StringComparer.OrdinalIgnoreCase) |
| 25 | + { |
| 26 | + "double", "System.Double", |
| 27 | + "single", "float", "System.Single", |
| 28 | + "decimal", "System.Decimal" |
| 29 | + }; |
| 30 | + |
| 31 | + private static readonly HashSet<string> IntegralTypes = new HashSet<string>(StringComparer.OrdinalIgnoreCase) |
| 32 | + { |
| 33 | + "byte", "System.Byte", |
| 34 | + "sbyte", "System.SByte", |
| 35 | + "short", "System.Int16", |
| 36 | + "ushort", "System.UInt16", |
| 37 | + "int", "System.Int32", |
| 38 | + "uint", "System.UInt32", |
| 39 | + "long", "System.Int64", |
| 40 | + "ulong", "System.UInt64" |
| 41 | + }; |
| 42 | + |
| 43 | + public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 44 | + { |
| 45 | + if (ast is null) |
| 46 | + { |
| 47 | + throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 48 | + } |
| 49 | + |
| 50 | + IEnumerable<Ast> parameterAsts = ast.FindAll(testAst => testAst is ParameterAst, searchNestedScriptBlocks: true); |
| 51 | + foreach (ParameterAst parameterAst in parameterAsts) |
| 52 | + { |
| 53 | + TypeConstraintAst typeConstraint = parameterAst.Attributes.OfType<TypeConstraintAst>().FirstOrDefault(); |
| 54 | + if (typeConstraint == null || !FloatingPointAndDecimalTypes.Contains(typeConstraint.TypeName.FullName)) |
| 55 | + { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + foreach (AttributeAst attributeAst in parameterAst.Attributes.OfType<AttributeAst>()) |
| 60 | + { |
| 61 | + if (!IsValidateRangeAttribute(attributeAst) |
| 62 | + || attributeAst.PositionalArguments.Count < 2 |
| 63 | + || !attributeAst.PositionalArguments.Take(2).Any(IsIntegralBoundary)) |
| 64 | + { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + yield return new DiagnosticRecord( |
| 69 | + String.Format(CultureInfo.CurrentCulture, Strings.AvoidValidateRangeTypeMismatchError, parameterAst.Name, typeConstraint.TypeName.FullName), |
| 70 | + attributeAst.Extent, |
| 71 | + GetName(), |
| 72 | + DiagnosticSeverity.Warning, |
| 73 | + fileName); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static bool IsIntegralBoundary(ExpressionAst expressionAst) |
| 79 | + { |
| 80 | + if (expressionAst is ConstantExpressionAst constantExpressionAst) |
| 81 | + { |
| 82 | + return constantExpressionAst.Value != null |
| 83 | + && IntegralTypes.Contains(constantExpressionAst.Value.GetType().FullName); |
| 84 | + } |
| 85 | + |
| 86 | + if (expressionAst is ConvertExpressionAst convertExpressionAst) |
| 87 | + { |
| 88 | + return IntegralTypes.Contains(convertExpressionAst.Type.TypeName.FullName) |
| 89 | + && IsIntegralBoundary(convertExpressionAst.Child); |
| 90 | + } |
| 91 | + |
| 92 | + if (expressionAst is UnaryExpressionAst unaryExpressionAst |
| 93 | + && (unaryExpressionAst.TokenKind == TokenKind.Minus || unaryExpressionAst.TokenKind == TokenKind.Plus)) |
| 94 | + { |
| 95 | + return IsIntegralBoundary(unaryExpressionAst.Child); |
| 96 | + } |
| 97 | + |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + private static bool IsValidateRangeAttribute(AttributeAst attributeAst) |
| 102 | + { |
| 103 | + string attributeName = attributeAst.TypeName.FullName; |
| 104 | + int lastDot = attributeName.LastIndexOf('.'); |
| 105 | + string simpleName = lastDot < 0 ? attributeName : attributeName.Substring(lastDot + 1); |
| 106 | + return String.Equals(simpleName, "ValidateRange", StringComparison.OrdinalIgnoreCase) |
| 107 | + || String.Equals(simpleName, "ValidateRangeAttribute", StringComparison.OrdinalIgnoreCase); |
| 108 | + } |
| 109 | + |
| 110 | + public string GetName() |
| 111 | + { |
| 112 | + return String.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidValidateRangeTypeMismatchName); |
| 113 | + } |
| 114 | + |
| 115 | + public string GetCommonName() |
| 116 | + { |
| 117 | + return Strings.AvoidValidateRangeTypeMismatchCommonName; |
| 118 | + } |
| 119 | + |
| 120 | + public string GetDescription() |
| 121 | + { |
| 122 | + return Strings.AvoidValidateRangeTypeMismatchDescription; |
| 123 | + } |
| 124 | + |
| 125 | + public SourceType GetSourceType() |
| 126 | + { |
| 127 | + return SourceType.Builtin; |
| 128 | + } |
| 129 | + |
| 130 | + public RuleSeverity GetSeverity() |
| 131 | + { |
| 132 | + return RuleSeverity.Warning; |
| 133 | + } |
| 134 | + |
| 135 | + public string GetSourceName() |
| 136 | + { |
| 137 | + return String.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments