Process.Close check for null before using variable (dotnet/corefx#32792)
authormatejskubic <839965+matejskubic@users.noreply.github.com>
Thu, 7 Feb 2019 18:36:36 +0000 (19:36 +0100)
committerStephen Toub <stoub@microsoft.com>
Thu, 7 Feb 2019 18:36:36 +0000 (13:36 -0500)
* Process.Close check for null before using

Fix when Process.Close throws Exception on redirected StandardOutput or StandardError.
Steps to reproduce

    process.Start
    process.BeginOutputReadLine
    process.Start
    process.Dispose

* Address PR feedback

Commit migrated from https://github.com/dotnet/corefx/commit/ff8f3149fc7aa1362b35b15da315646c3bb48dc9

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs
src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs

index 7b2f529..7fbf943 100644 (file)
@@ -861,7 +861,7 @@ namespace System.Diagnostics
                     {
                         if (_outputStreamReadMode == StreamReadMode.AsyncMode)
                         {
-                            _output.CancelOperation();
+                            _output?.CancelOperation();
                         }
                         _standardOutput.Close();
                     }
@@ -870,7 +870,7 @@ namespace System.Diagnostics
                     {
                         if (_errorStreamReadMode == StreamReadMode.AsyncMode)
                         {
-                            _error.CancelOperation();
+                            _error?.CancelOperation();
                         }
                         _standardError.Close();
                     }
index 9d0ffa3..2ff86d2 100644 (file)
@@ -1370,6 +1370,24 @@ namespace System.Diagnostics.Tests
         }
 
         [Fact]
+        public void Start_RedirectStandardOutput_StartAgain_DoesntThrow()
+        {
+            using (Process process = CreateProcess(() =>
+            {
+                Console.WriteLine("hello world");
+                return SuccessExitCode;
+            }))
+            {
+                process.StartInfo.RedirectStandardOutput = true;
+
+                Assert.True(process.Start());
+                process.BeginOutputReadLine();
+
+                Assert.True(process.Start());
+            }
+        }
+
+        [Fact]
         public void Start_Disposed_ThrowsObjectDisposedException()
         {
             var process = new Process();