Skip to content

Improve overflow detection algorithm in XML documentation examples #47975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 15, 2025

The XML documentation examples in include-tag.cs contained a flawed overflow detection algorithm that only caught the specific case where one parameter equals int.MaxValue and the other is positive. This approach missed many overflow scenarios like (int.MaxValue - 5) + 10.

Changes Made

Integer Add Method

Replaced the manual overflow check with .NET's built-in checked context:

// Before: Limited manual check
if ((a == int.MaxValue && b > 0) || (b == int.MaxValue && a > 0))
    throw new System.OverflowException();
return a + b;

// After: Comprehensive overflow detection
try
{
    return checked(a + b);
}
catch (OverflowException)
{
    throw new System.OverflowException();
}

Double Add Method

Enhanced floating-point overflow detection with proper infinity checks:

// Added comprehensive checks for infinity and overflow conditions
if (double.IsInfinity(a) || double.IsInfinity(b) || 
    (a == double.MaxValue && b > 0) || (b == double.MaxValue && a > 0))
    throw new System.OverflowException();

double result = a + b;
if (double.IsInfinity(result))
    throw new System.OverflowException();

Benefits

  • More accurate: Uses .NET's comprehensive overflow detection instead of incomplete manual checks
  • Educational value: Demonstrates proper .NET overflow handling techniques using checked context
  • Maintains documentation focus: All XML documentation elements remain intact, preserving the example's primary purpose of demonstrating <include> tags and exception documentation
  • Simpler: Less custom logic, leverages framework capabilities

The changes align with maintainer feedback suggesting the use of checked(a + b) while keeping the example focused on XML documentation features rather than complex mathematical algorithms.

Fixes #41596.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Possibly flawed algorithm Improve overflow detection algorithm in XML documentation examples Aug 15, 2025
@Copilot Copilot AI requested a review from BillWagner August 15, 2025 14:55
Copilot finished work on behalf of BillWagner August 15, 2025 14:55
Copy link
Contributor

PoliCheck Scan Report

The following report lists PoliCheck issues in PR files. Before you merge the PR, you must fix all severity-1 issues. Other issues are also a high priority. The AI Review Details column lists suggestions for either removing or replacing the terms. If you find a false positive result, mention it in a PR comment and include this text: #policheck-false-positive. This feedback helps reduce false positives in future scans.

✅ No issues found

More information about PoliCheck

Information: PoliCheck | Severity Guidance | Term
For any questions: Try searching the learn.microsoft.com contributor guides or post your question in the Learn support channel.

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.

Possibly flawed algorithm
2 participants