Skip to content

Commit

Permalink
Make regexes more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 committed Dec 19, 2024
1 parent 8cd69e8 commit f478cd8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
16 changes: 16 additions & 0 deletions AssetRipper.Primitives.Tests/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ public void ShortChineseVersionFormat()
});
}

[TestCase("g2019")]
[TestCase("g2019.4")]
[TestCase("g2019.4.3")]
[TestCase("g2019.4.3f")]
[TestCase("g2019.4.3f1")]
[TestCase("g2019.4.3f1c1")]
[TestCase("2019g")]
[TestCase("2019.4g")]
[TestCase("2019.4.3g")]
[TestCase("2019.4.3fg")]
// "2019.4.3f1g" and "2019.4.3f1c1g" are technically valid because custom engines often append some text to the version string.
public void InvalidVersionStringFailsToParse(string versionString)
{
Assert.That(UnityVersion.TryParse(versionString, out _, out _), Is.False);
}

private static IEnumerable<UnityVersion> GenerateRandomVersions(int count)
{
Randomizer random = TestContext.CurrentContext.Random;
Expand Down
10 changes: 5 additions & 5 deletions AssetRipper.Primitives/UnityVersionRegexes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ internal static partial class UnityVersionRegexes
#endif

[StringSyntax("regex")]
private const string Major = @"([0-9]+)";
private const string Major = @"^([0-9]+)$";

[StringSyntax("regex")]
private const string MajorMinor = @"([0-9]+)\.([0-9]+)";
private const string MajorMinor = @"^([0-9]+)\.([0-9]+)$";

[StringSyntax("regex")]
private const string MajorMinorBuild = @"([0-9]+)\.([0-9]+)\.([0-9]+)";
private const string MajorMinorBuild = @"^([0-9]+)\.([0-9]+)\.([0-9]+)$";

[StringSyntax("regex")]
private const string Normal = @"([0-9]+)\.([0-9]+)\.([0-9]+)\.?([abcfpx])([0-9]+)((?:.|[\r\n])+)?";
private const string Normal = @"^([0-9]+)\.([0-9]+)\.([0-9]+)\.?([abcfpx])([0-9]+)((?:.|[\r\n])+)?$";

[StringSyntax("regex")]
private const string China = @"([0-9]+)\.([0-9]+)\.([0-9]+)\.?f1c([0-9]+)((?:.|[\r\n])+)?";
private const string China = @"^([0-9]+)\.([0-9]+)\.([0-9]+)\.?f1c([0-9]+)((?:.|[\r\n])+)?$";

#if NET7_0_OR_GREATER
[GeneratedRegex(Major, Options)]
Expand Down

0 comments on commit f478cd8

Please sign in to comment.