Skip to content

Replace int.Parse() with int.TryParse() to avoid exceptions caused by overflowing values in tags #2703

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

Closed
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace GitVersion
public class SemanticVersion : IFormattable, IComparable<SemanticVersion>
{
private static SemanticVersion Empty = new SemanticVersion();

private static readonly Regex ParseSemVer = new Regex(
@"^(?<SemVer>(?<Major>\d+)?(\.(?<Minor>\d+))?(\.(?<Patch>\d+))?)(\.(?<FourthPart>\d+))?(-(?<Tag>[^\+]*))?(\+(?<BuildMetaData>.*))?$",
RegexOptions.Compiled);
Expand Down Expand Up @@ -147,10 +146,10 @@ public static SemanticVersion Parse(string version, string tagPrefixRegex)
public static bool TryParse(string version, string tagPrefixRegex, out SemanticVersion semanticVersion)
{
var match = Regex.Match(version, $"^({tagPrefixRegex})?(?<version>.*)$");
semanticVersion = null;

if (!match.Success)
{
semanticVersion = null;
return false;
}

Expand All @@ -159,29 +158,47 @@ public static bool TryParse(string version, string tagPrefixRegex, out SemanticV

if (!parsed.Success)
{
semanticVersion = null;
return false;
}

var semanticVersionBuildMetaData = SemanticVersionBuildMetaData.Parse(parsed.Groups["BuildMetaData"].Value);
var fourthPart = parsed.Groups["FourthPart"];
if (fourthPart.Success && semanticVersionBuildMetaData.CommitsSinceTag == null)
{
semanticVersionBuildMetaData.CommitsSinceTag = int.Parse(fourthPart.Value);
semanticVersionBuildMetaData.CommitsSinceTag = Int32.Parse(fourthPart.Value);
}

int major = 0, minor = 0, patch = 0;

if (!parsed.Groups["Major"].Success || !Int32.TryParse(parsed.Groups["Major"].Value, out major))
{
return false;
}

if (parsed.Groups["Minor"].Success && !Int32.TryParse(parsed.Groups["Minor"].Value, out minor))
{
return false;
}

if (parsed.Groups["Patch"].Success && !Int32.TryParse(parsed.Groups["Patch"].Value, out patch))
{
return false;
}

semanticVersion = new SemanticVersion
{
Major = int.Parse(parsed.Groups["Major"].Value),
Minor = parsed.Groups["Minor"].Success ? int.Parse(parsed.Groups["Minor"].Value) : 0,
Patch = parsed.Groups["Patch"].Success ? int.Parse(parsed.Groups["Patch"].Value) : 0,
Major = major,
Minor = minor,
Patch = patch,
PreReleaseTag = SemanticVersionPreReleaseTag.Parse(parsed.Groups["Tag"].Value),
BuildMetaData = semanticVersionBuildMetaData
};

return true;
}



public int CompareTo(SemanticVersion value)
{
return CompareTo(value, true);
Expand Down