Skip to content

Commit

Permalink
[GB18030] Fix SubstringByTextElements - avoid `ArgumentOutOfRangeEx…
Browse files Browse the repository at this point in the history
…ception` (#10080)

* Fix out of range exception.

* Update src/Build/Evaluation/IntrinsicFunctions.cs

Co-authored-by: Jan Krivanek <krivanek.j@hotmail.com>

* Behavior depends on .net version by design.

* Test per-framework solution.

* Simplification

* Feedback

---------

Co-authored-by: Jan Krivanek <krivanek.j@hotmail.com>
  • Loading branch information
ilonatommy and JanKrivanek authored Apr 30, 2024
1 parent 5e9668d commit ab42402
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/Build.UnitTests/Evaluation/Expander_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4383,13 +4383,13 @@ public void PropertyFunctionCheckFeatureAvailability(string featureName, string
}

[Theory]
[InlineData("\u3407\ud840\udc60\ud86a\ude30\ud86e\udc0a\ud86e\udda0\ud879\udeae\u2fd5\u0023", 0, 3, "\u3407\ud840\udc60\ud86a\ude30")]
[InlineData("\u3407\ud840\udc60\ud86a\ude30\ud86e\udc0a\ud86e\udda0\ud879\udeae\u2fd5\u0023", 2, 5, "\ud86a\ude30\ud86e\udc0a\ud86e\udda0\ud879\udeae\u2fd5")]
public void SubstringByTextElements(string featureName, int start, int length, string expected)
[InlineData("\u0074\u0068\u0069\u0073\u002a\u3407\ud840\udc60\ud86a\ude30\ud86e\udc0a\ud86e\udda0\ud879\udeae\u2fd5\u0023", 2, 10, "is________")]
[InlineData("\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc66\u200d\ud83d\udc66\ud83d\udc68\u200d\ud83d\udc68\u200d\ud83d\udc66\u200d\ud83d\udc66\u002e\u0070\u0072\u006f\u006a", 0, 8, "________")]
public void SubstringByAsciiChars(string featureName, int start, int length, string expected)
{
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(new PropertyDictionary<ProjectPropertyInstance>(), FileSystems.Default);

var result = expander.ExpandIntoStringLeaveEscaped($"$([MSBuild]::SubstringByTextElements({featureName}, {start}, {length}))", ExpanderOptions.ExpandProperties, MockElementLocation.Instance);
var result = expander.ExpandIntoStringLeaveEscaped($"$([MSBuild]::SubstringByAsciiChars({featureName}, {start}, {length}))", ExpanderOptions.ExpandProperties, MockElementLocation.Instance);

Assert.Equal(expected, result);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Build/Evaluation/Expander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4229,11 +4229,11 @@ private bool TryExecuteWellKnownFunction(out object returnVal, object objectInst
return true;
}
}
else if (string.Equals(_methodMethodName, nameof(IntrinsicFunctions.SubstringByTextElements), StringComparison.OrdinalIgnoreCase))
else if (string.Equals(_methodMethodName, nameof(IntrinsicFunctions.SubstringByAsciiChars), StringComparison.OrdinalIgnoreCase))
{
if (TryGetArgs(args, out string arg0, out int arg1, out int arg2))
{
returnVal = IntrinsicFunctions.SubstringByTextElements(arg0, arg1, arg2);
returnVal = IntrinsicFunctions.SubstringByAsciiChars(arg0, arg1, arg2);
return true;
}
}
Expand Down
28 changes: 24 additions & 4 deletions src/Build/Evaluation/IntrinsicFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Runtime.Versioning;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Experimental.BuildCheck;
using Microsoft.Build.Framework;
Expand All @@ -19,6 +18,7 @@
using Microsoft.Build.Utilities;
using Microsoft.NET.StringTools;
using Microsoft.Win32;
using System.Linq;

// Needed for DoesTaskHostExistForParameters
using NodeProviderOutOfProcTaskHost = Microsoft.Build.BackEnd.NodeProviderOutOfProcTaskHost;
Expand Down Expand Up @@ -629,10 +629,30 @@ internal static bool AreFeaturesEnabled(Version wave)
return ChangeWaves.AreFeaturesEnabled(wave);
}

internal static string SubstringByTextElements(string input, int start, int length)
internal static string SubstringByAsciiChars(string input, int start, int length)
{
StringInfo stringInfo = new StringInfo(input);
return stringInfo.SubstringByTextElements(start, length);
if (start > input.Length)
{
return string.Empty;
}
if (start + length > input.Length)
{
length = input.Length - start;
}
StringBuilder sb = new StringBuilder();
for (int i = start; i < start + length; i++)
{
char c = input[i];
if (c >= 32 && c <= 126 && !FileUtilities.InvalidFileNameChars.Contains(c))
{
sb.Append(c);
}
else
{
sb.Append('_');
}
}
return sb.ToString();
}

internal static string CheckFeatureAvailability(string featureName)
Expand Down
2 changes: 1 addition & 1 deletion src/Tasks/Microsoft.Common.CurrentVersion.targets
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<PropertyGroup Condition="'$(MSBuildCopyMarkerName)' == ''">
<MSBuildCopyMarkerName>$(MSBuildProjectFile)</MSBuildCopyMarkerName>
<!-- For a long MSBuildProjectFile let's shorten this to 17 chars - using the first 8 codepoints of the filename and a filename hash. -->
<MSBuildCopyMarkerName Condition="'$(MSBuildCopyMarkerName.Length)' &gt; '17'">$([MSBuild]::SubstringByTextElements($(MSBuildProjectFile), 0, 8)).$([MSBuild]::StableStringHash($(MSBuildProjectFile)).ToString("X8"))</MSBuildCopyMarkerName>
<MSBuildCopyMarkerName Condition="'$(MSBuildCopyMarkerName.Length)' &gt; '17'">$([MSBuild]::SubstringByAsciiChars($(MSBuildProjectFile), 0, 8)).$([MSBuild]::StableStringHash($(MSBuildProjectFile)).ToString("X8"))</MSBuildCopyMarkerName>
<MSBuildCopyMarkerName>$(MSBuildCopyMarkerName).Up2Date</MSBuildCopyMarkerName>
</PropertyGroup>

Expand Down

0 comments on commit ab42402

Please sign in to comment.