Skip to content
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

[GB18030] Fix SubstringByTextElements - avoid ArgumentOutOfRangeException #10080

Merged
merged 6 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
YuliiaKovalova marked this conversation as resolved.
Show resolved Hide resolved

// 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