Skip to content

Commit fd67038

Browse files
authored
Replace UTF8Encoding(false) with Encoding.Default (PowerShell#18144)
1 parent dfa012d commit fd67038

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

src/System.Management.Automation/engine/InitialSessionState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4340,7 +4340,7 @@ static InitialSessionState()
43404340
// Variable which controls the encoding for piping data to a NativeCommand
43414341
new SessionStateVariableEntry(
43424342
SpecialVariables.OutputEncoding,
4343-
Utils.utf8NoBom,
4343+
Encoding.Default,
43444344
RunspaceInit.OutputEncodingDescription,
43454345
ScopedItemOptions.None,
43464346
new ArgumentTypeConverterAttribute(typeof(System.Text.Encoding))),

src/System.Management.Automation/engine/NativeCommandProcessor.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,11 +2057,12 @@ internal void Start(Process process, NativeCommandIOFormat inputFormat)
20572057
// Get the encoding for writing to native command. Note we get the Encoding
20582058
// from the current scope so a script or function can use a different encoding
20592059
// than global value.
2060-
Encoding pipeEncoding = _command.Context.GetVariableValue(SpecialVariables.OutputEncodingVarPath) as System.Text.Encoding ??
2061-
Utils.utf8NoBom;
2060+
Encoding outputEncoding = _command.Context.GetVariableValue(SpecialVariables.OutputEncodingVarPath) as Encoding;
20622061

2063-
_streamWriter = new StreamWriter(process.StandardInput.BaseStream, pipeEncoding);
2064-
_streamWriter.AutoFlush = true;
2062+
_streamWriter = new StreamWriter(process.StandardInput.BaseStream, outputEncoding ?? Encoding.Default)
2063+
{
2064+
AutoFlush = true
2065+
};
20652066

20662067
_inputFormat = inputFormat;
20672068

src/System.Management.Automation/engine/Utils.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,9 +1380,6 @@ internal static bool Succeeded(int hresult)
13801380
// Add-Member ScriptProperty Preamble { $this.GetEncoding().GetPreamble() -join "-" } -PassThru |
13811381
// Format-Table -Auto
13821382

1383-
internal static readonly UTF8Encoding utf8NoBom =
1384-
new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
1385-
13861383
#if !UNIX
13871384
/// <summary>
13881385
/// Queues a CLR worker thread with impersonation of provided Windows identity.

src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ internal void FlushContentToDisk()
12431243
{
12441244
static Encoding GetPathEncoding(string path)
12451245
{
1246-
using StreamReader reader = new StreamReader(path, Utils.utf8NoBom, detectEncodingFromByteOrderMarks: true);
1246+
using StreamReader reader = new StreamReader(path, Encoding.Default, detectEncodingFromByteOrderMarks: true);
12471247
_ = reader.Read();
12481248
return reader.CurrentEncoding;
12491249
}
@@ -1271,7 +1271,7 @@ static Encoding GetPathEncoding(string path)
12711271
// file permissions.
12721272
_contentWriter = new StreamWriter(
12731273
new FileStream(this.Path, FileMode.Append, FileAccess.Write, FileShare.Read),
1274-
Utils.utf8NoBom);
1274+
Encoding.Default);
12751275
}
12761276

12771277
_contentWriter.AutoFlush = true;

src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Reflection;
1919
using System.Runtime.InteropServices;
2020
using System.Security.AccessControl;
21+
using System.Text;
2122
using System.Threading;
2223

2324
using Microsoft.Win32.SafeHandles;
@@ -2404,23 +2405,31 @@ internal static int StartSSHProcess(
24042405
if (startInfo.RedirectStandardInput)
24052406
{
24062407
Debug.Assert(stdinFd >= 0, "Invalid Fd");
2407-
standardInput = new StreamWriter(OpenStream(stdinFd, FileAccess.Write),
2408-
Utils.utf8NoBom, StreamBufferSize)
2408+
standardInput = new StreamWriter(
2409+
OpenStream(stdinFd, FileAccess.Write),
2410+
Encoding.Default,
2411+
StreamBufferSize)
24092412
{ AutoFlush = true };
24102413
}
24112414

24122415
if (startInfo.RedirectStandardOutput)
24132416
{
24142417
Debug.Assert(stdoutFd >= 0, "Invalid Fd");
2415-
standardOutput = new StreamReader(OpenStream(stdoutFd, FileAccess.Read),
2416-
startInfo.StandardOutputEncoding ?? Utils.utf8NoBom, true, StreamBufferSize);
2418+
standardOutput = new StreamReader(
2419+
OpenStream(stdoutFd, FileAccess.Read),
2420+
startInfo.StandardOutputEncoding ?? Encoding.Default,
2421+
detectEncodingFromByteOrderMarks: true,
2422+
StreamBufferSize);
24172423
}
24182424

24192425
if (startInfo.RedirectStandardError)
24202426
{
24212427
Debug.Assert(stderrFd >= 0, "Invalid Fd");
2422-
standardError = new StreamReader(OpenStream(stderrFd, FileAccess.Read),
2423-
startInfo.StandardErrorEncoding ?? Utils.utf8NoBom, true, StreamBufferSize);
2428+
standardError = new StreamReader(
2429+
OpenStream(stderrFd, FileAccess.Read),
2430+
startInfo.StandardErrorEncoding ?? Encoding.Default,
2431+
detectEncodingFromByteOrderMarks: true,
2432+
StreamBufferSize);
24242433
}
24252434

24262435
return childPid;

0 commit comments

Comments
 (0)