Loris S cd03a1dd3d
Modify S5144&S6547: Improve fixes (#2912)
## Review

A dedicated reviewer checked the rule description successfully for:

- [ ] logical errors and incorrect information
- [ ] information gaps and missing content
- [ ] text style and tone
- [ ] PR summary and labels follow [the
guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule)
2023-08-21 10:51:21 +02:00

52 lines
1.1 KiB
Plaintext

== How to fix it in .NET
=== Code examples
include::../../common/fix/code-rationale.adoc[]
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
using System.Diagnostics;
public class ExampleController : Controller
{
public void Example(string name, string value)
{
Process proc = new Process();
proc.StartInfo.FileName = "path/to/executable";
proc.StartInfo.EnvironmentVariables.Add(name, value); // Noncompliant
proc.Start();
}
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
using System.Diagnostics;
using System.Text.RegularExpressions;
public class ExampleController : Controller
{
public void Example(string value)
{
Process proc = new Process();
proc.StartInfo.FileName = "path/to/executable";
string pattern = "^[a-zA-Z0-9]*$";
Match m = Regex.Match(value, pattern);
if (m.Success) {
proc.StartInfo.EnvironmentVariables.Add("ENV_VAR", value);
}
proc.Start();
}
}
----
=== How does this work?
include::../../common/fix/pre-approved-list.adoc[]