-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Fix Version.Parse to show original input in FormatException message when input has trailing dots
#121055
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
base: main
Are you sure you want to change the base?
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-runtime |
…or trailing dots Co-authored-by: stephentoub <[email protected]>
Version.Parse to show original input in FormatException message when input has trailing dots
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/VersionTests.cs
Show resolved
Hide resolved
…add UTF8 invalid byte tests Co-authored-by: stephentoub <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a bug where Version.Parse displayed an empty string in error messages when parsing failed due to trailing dots (e.g., "1.0."). The fix ensures the original input string is included in FormatException messages for better diagnostics.
Key Changes:
- Modified
TryParseComponentto accept and use the original input string for error messages - Refactored error handling to use non-throwing
TryParseBinaryIntegerStylefor better performance - Added comprehensive test coverage for trailing dot scenarios and invalid UTF8 sequences
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/Version.cs | Updated TryParseComponent to accept original input parameter and refactored exception throwing to a [DoesNotReturn] static local function |
| src/libraries/System.Runtime/tests/System.Runtime.Tests/System/VersionTests.cs | Added tests for trailing dots with string/span/UTF8 inputs and invalid UTF8 byte sequences |
|
@EgorBot -amd -intel -arm using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkSwitcher.FromAssembly(typeof(Benchmarks).Assembly).Run(args);
public class Benchmarks
{
[Benchmark]
[Arguments("1.2")]
[Arguments("123.123.123.123")]
public Version Parse(string s) => Version.Parse(s);
} |
|
@EgorBo, looks like your bot might need to bump some version numbers? |
Co-authored-by: stephentoub <[email protected]>
| if (parsedComponent < 0) | ||
| { | ||
| ArgumentOutOfRangeException.ThrowIfNegative(parsedComponent, componentName); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot, this if condition should not be necessary, as it's going to be checked by ThrowIfNegative
| if (parsedComponent < 0) | |
| { | |
| ArgumentOutOfRangeException.ThrowIfNegative(parsedComponent, componentName); | |
| } | |
| ArgumentOutOfRangeException.ThrowIfNegative(parsedComponent, componentName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit 2fcaa84. Removed the unnecessary if (parsedComponent < 0) condition since ThrowIfNegative already performs this check.
Co-authored-by: stephentoub <[email protected]>
Head branch was pushed to by a user without write access
Summary
Fixed
Version.Parseto include the original input string inFormatExceptionmessages when parsing fails due to trailing dots or empty components.Changes:
TryParseComponentto accept original input parameterNumber.TryParseBinaryIntegerStylepath for better performance[DoesNotReturn]static local functionThrowIfNegativealready performs the checkVerification:
Original prompt
Fixes #115612
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.