Skip to content

Commit f918d2a

Browse files
Remove illegal string manipulation (#2531)
1 parent b4485d8 commit f918d2a

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

src/System.CommandLine/Parsing/StringExtensions.cs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -225,44 +225,38 @@ bool TryUnbundle(ReadOnlySpan<char> alias, int argumentIndex)
225225
{
226226
int tokensBefore = tokenList.Count;
227227

228-
string candidate = new('-', 2); // mutable string used to avoid allocations
229-
unsafe
228+
Span<char> candidate = ['-', '-'];
229+
for (int i = 0; i < alias.Length; i++)
230230
{
231-
fixed (char* pCandidate = candidate)
231+
if (alias[i] == ':' || alias[i] == '=')
232232
{
233-
for (int i = 0; i < alias.Length; i++)
234-
{
235-
if (alias[i] == ':' || alias[i] == '=')
236-
{
237-
tokenList.Add(new Token(alias.Slice(i + 1).ToString(), TokenType.Argument, default, argumentIndex));
238-
return true;
239-
}
233+
tokenList.Add(new Token(alias.Slice(i + 1).ToString(), TokenType.Argument, default, argumentIndex));
234+
return true;
235+
}
240236

241-
pCandidate[1] = alias[i];
242-
if (!knownTokens.TryGetValue(candidate, out Token? found))
243-
{
244-
if (tokensBefore != tokenList.Count && tokenList[tokenList.Count - 1].Type == TokenType.Option)
245-
{
246-
// Invalid_char_in_bundle_causes_rest_to_be_interpreted_as_value
247-
tokenList.Add(new Token(alias.Slice(i).ToString(), TokenType.Argument, default, argumentIndex));
248-
return true;
249-
}
237+
candidate[1] = alias[i];
238+
if (!knownTokens.TryGetValue(candidate.ToString(), out Token? found))
239+
{
240+
if (tokensBefore != tokenList.Count && tokenList[tokenList.Count - 1].Type == TokenType.Option)
241+
{
242+
// Invalid_char_in_bundle_causes_rest_to_be_interpreted_as_value
243+
tokenList.Add(new Token(alias.Slice(i).ToString(), TokenType.Argument, default, argumentIndex));
244+
return true;
245+
}
250246

251-
return false;
252-
}
247+
return false;
248+
}
253249

254-
tokenList.Add(new Token(found.Value, found.Type, found.Symbol, argumentIndex));
255-
if (i != alias.Length - 1 && ((Option)found.Symbol!).Greedy)
256-
{
257-
int index = i + 1;
258-
if (alias[index] == ':' || alias[index] == '=')
259-
{
260-
index++; // Last_bundled_option_can_accept_argument_with_colon_separator
261-
}
262-
tokenList.Add(new Token(alias.Slice(index).ToString(), TokenType.Argument, default, argumentIndex));
263-
return true;
264-
}
250+
tokenList.Add(new Token(found.Value, found.Type, found.Symbol, argumentIndex));
251+
if (i != alias.Length - 1 && ((Option)found.Symbol!).Greedy)
252+
{
253+
int index = i + 1;
254+
if (alias[index] == ':' || alias[index] == '=')
255+
{
256+
index++; // Last_bundled_option_can_accept_argument_with_colon_separator
265257
}
258+
tokenList.Add(new Token(alias.Slice(index).ToString(), TokenType.Argument, default, argumentIndex));
259+
return true;
266260
}
267261
}
268262

src/System.CommandLine/System.CommandLine.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<PackageId>System.CommandLine</PackageId>
66
<TargetFrameworks>$(TargetFrameworkForNETSDK);netstandard2.0</TargetFrameworks>
77
<Nullable>enable</Nullable>
8-
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
98
<LangVersion>latest</LangVersion>
109
<Description>Support for parsing command lines, supporting both POSIX and Windows conventions and shell-agnostic command line completions.</Description>
1110
<GenerateDocumentationFile>true</GenerateDocumentationFile>

0 commit comments

Comments
 (0)