Patchwork D11928: procutil: avoid an uninitialized variable usage on tempfile exception

login
register
mail settings
Submitter phabricator
Date Dec. 15, 2021, 8:47 p.m.
Message ID <differential-rev-PHID-DREV-pw5tssb4wbxb2gnqfylr-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50253/
State New
Headers show

Comments

phabricator - Dec. 15, 2021, 8:47 p.m.
mharbison72 created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  If `pycompat.unnamedtempfile()` raises an exception, it would have called
  `stdin.close()` in the `finally` block without it being initialized first.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D11928

AFFECTED FILES
  mercurial/utils/procutil.py

CHANGE DETAILS




To: mharbison72, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/utils/procutil.py b/mercurial/utils/procutil.py
--- a/mercurial/utils/procutil.py
+++ b/mercurial/utils/procutil.py
@@ -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)