Skip to content

Fix int.MinValue overflow and CTS disposal in DistributedTracingSample#649

Merged
torosent merged 2 commits intotorosent/add-otel-samplefrom
copilot/sub-pr-637
Feb 27, 2026
Merged

Fix int.MinValue overflow and CTS disposal in DistributedTracingSample#649
torosent merged 2 commits intotorosent/add-otel-samplefrom
copilot/sub-pr-637

Conversation

Copy link
Contributor

Copilot AI commented Feb 27, 2026

Two correctness issues in the DistributedTracingSample GetWeather activity identified during review.

Changes

  • Math.Abs overflow (Program.cs): Math.Abs(int.MinValue) overflows back to int.MinValue (still negative), causing IndexOutOfRangeException on the modulo. Replaced with a uint cast, which is both overflow-safe and removes the double GetHashCode() call:

    // Before — unsafe
    string condition = conditions[Math.Abs(city.GetHashCode()) % conditions.Length];
    int temperature = 15 + (Math.Abs(city.GetHashCode()) % 20);
    
    // After — safe, single hash evaluation
    uint hash = (uint)city.GetHashCode();
    string condition = conditions[hash % conditions.Length];
    int temperature = 15 + (int)(hash % 20);
  • CancellationTokenSource disposal: Added using declaration so the underlying timer is released when the token source goes out of scope.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: torosent <17064840+torosent@users.noreply.github.com>
Copilot AI changed the title [WIP] Add OpenTelemetry sample and update dependencies Fix int.MinValue overflow and CTS disposal in DistributedTracingSample Feb 27, 2026
@torosent torosent marked this pull request as ready for review February 27, 2026 19:58
@torosent torosent merged commit 0644db7 into torosent/add-otel-sample Feb 27, 2026
3 checks passed
@torosent torosent deleted the copilot/sub-pr-637 branch February 27, 2026 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants