Skip to content

Warn on integer ValidateRange bounds for floating-point parameters - #2267

Open
Christoph Bergmeister (bergmeister) with Copilot wants to merge 5 commits into
mainfrom
copilot/validate-range-boundary-check
Open

Christoph Bergmeister (bergmeister) with Copilot wants to merge 5 commits into
mainfrom
copilot/validate-range-boundary-check

Conversation

Copilot AI commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Integer ValidateRange bounds can allow fractional values outside the declared range on double, single, and decimal parameters.

  • Analyzer rule: Add an always-enabled warning for integral bounds on these parameter types.
  • Coverage and docs: Add focused tests and document the rule.
# Warning: integer bounds can allow fractional values outside the range
param([double][ValidateRange(0, 100)]$Value)

# Use floating-point bounds
param([double][ValidateRange(0.0, 100.0)]$Value)

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
Copilot AI changed the title [WIP] Add type check for ValidateRange boundaries Warn on integer ValidateRange bounds for floating-point parameters Oct 27, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The rule misses affected array parameters and integral-cast bounds while incorrectly warning on mixed integer/floating-point bounds.

3 open issues
What changed in this PR

Adds a built-in PSScriptAnalyzer warning for integer ValidateRange bounds on floating-point and decimal parameters, addressing the boundary-validation concern in #2199.

Changes:

  • Introduces the always-enabled PSAvoidValidateRangeTypeMismatch warning and diagnostic messages.
  • Adds tests for supported parameter types, boundary literals, and qualified attribute names.
  • Documents the rule with examples and adds it to the rule index.
File Description
Tests/​Rules/​AvoidValidateRangeTypeMismatch.tests.ps1 Adds warning and no-warning test cases.
Rules/​Strings.resx Adds rule metadata and diagnostic text.
Rules/​AvoidValidateRangeTypeMismatch.cs Implements parameter and boundary-type checks.
docs/​Rules/​README.md Lists the new warning rule.
docs/​Rules/​AvoidValidateRangeTypeMismatch.md Explains the rule, examples, and suppression options.

Review effort: Balanced

Comment thread Rules/AvoidValidateRangeTypeMismatch.cs Outdated
Comment thread Rules/AvoidValidateRangeTypeMismatch.cs Outdated
Comment thread Rules/AvoidValidateRangeTypeMismatch.cs Outdated
* Initial plan

* Fix ValidateRange analyzer review findings

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

* Restore decimal range test coverage

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The rule misses static integral constant bounds and incorrectly flags unrelated custom ValidateRange attributes, causing both false negatives and false positives.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Match the resolved ValidateRangeAttribute type

Rules/​AvoidValidateRangeTypeMismatch.cs:121

Discarding the namespace also matches unrelated custom attributes. For example, [Contoso.ValidateRange(0, 100)] on a double parameter triggers this warning even when that attribute is not PowerShell's range validator and has different semantics. Compare the resolved attribute type with System.Management.Automation.ValidateRangeAttribute instead; this supports both short and fully qualified built-in names without reporting unrelated attributes.

3 resolved since last review

Review effort: Balanced

Comment thread Rules/AvoidValidateRangeTypeMismatch.cs
* Initial plan

* Handle ValidateRange static integral bounds

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

* Fix ValidateRange review edge cases

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

* Add real Contoso.ValidateRangeAttribute to test fixture

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

* Test non-constant ValidateRange bounds

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The rule skips nullable floating-point parameters, leaving fractional values outside integer ValidateRange bounds undetected; nullable types need unwrapping and coverage for double, single, and decimal parameters.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Handle nullable floating-point parameters in range validation

Rules/​AvoidValidateRangeTypeMismatch.cs:71

Nullable floating-point parameters are skipped here: param([Nullable[double]][ValidateRange(0, 100)]$Value) resolves to Nullable<double>, which is not in this set. A non-null nullable value is boxed as its underlying numeric type, so integer bounds still permit out-of-range fractional values. Unwrap nullable types before this check and add coverage for nullable double, single, and decimal parameters.

1 resolved since last review

Review effort: Balanced

@bergmeister

Copy link
Copy Markdown
Collaborator

🔵 Needs a closer look

The rule skips nullable floating-point parameters, leaving fractional values outside integer ValidateRange bounds undetected; nullable types need unwrapping and coverage for double, single, and decimal parameters.

No open issues 🎉

1 previously missed issue
In code that hasn't changed since last review

Medium severity Handle nullable floating-point parameters in range validation
Rules/​AvoidValidateRangeTypeMismatch.cs:71

Nullable floating-point parameters are skipped here: param([Nullable[double]][ValidateRange(0, 100)]$Value) resolves to Nullable<double>, which is not in this set. A non-null nullable value is boxed as its underlying numeric type, so integer bounds still permit out-of-range fractional values. Unwrap nullable types before this check and add coverage for nullable double, single, and decimal parameters.

1 resolved since last review

Review effort: Balanced

Copilot fix

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

Copilot AI commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

🔵 Needs a closer look

The rule skips nullable floating-point parameters, leaving fractional values outside integer ValidateRange bounds undetected; nullable types need unwrapping and cover...

Nullable double, single, and decimal parameter types are now unwrapped before checking; regression tests pass. Fixed in 32bcb67.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because parenthesized integer bounds can bypass the rule’s warning, potentially allowing fractional values outside the declared range.

No open issues 🎉

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ValidateRange boundary type vs parameter type check

3 participants