changeset 48488:f8540fe4be0f

procutil: avoid an uninitialized variable usage on tempfile exception If `pycompat.unnamedtempfile()` raises an exception, it would have called `stdin.close()` in the `finally` block without it being initialized first. Differential Revision: https://phab.mercurial-scm.org/D11928
author Matt Harbison <matt_harbison@yahoo.com>
date Tue, 14 Dec 2021 17:29:30 -0500
parents 333a2656e981
children 12a43f857a11
files mercurial/utils/procutil.py
diffstat 1 files changed, 7 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/utils/procutil.py	Tue Dec 14 17:25:46 2021 -0500
+++ b/mercurial/utils/procutil.py	Tue Dec 14 17:29:30 2021 -0500
@@ -742,6 +742,8 @@
             start_new_session = False
             ensurestart = True
 
+        stdin = None
+
         try:
             if stdin_bytes is None:
                 stdin = subprocess.DEVNULL
@@ -770,7 +772,7 @@
                 record_wait(255)
             raise
         finally:
-            if stdin_bytes is not None:
+            if stdin_bytes is not None and stdin is not None:
                 assert not isinstance(stdin, int)
                 stdin.close()
         if not ensurestart:
@@ -852,6 +854,8 @@
                 return
 
         returncode = 255
+        stdin = None
+
         try:
             if record_wait is None:
                 # Start a new session
@@ -894,7 +898,8 @@
         finally:
             # mission accomplished, this child needs to exit and not
             # continue the hg process here.
-            stdin.close()
+            if stdin is not None:
+                stdin.close()
             if record_wait is None:
                 os._exit(returncode)