comparison mercurial/utils/procutil.py @ 48527: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 90e564882f07 6000f5b25c9b
comparison
equal deleted inserted replaced
48526:333a2656e981 48527:f8540fe4be0f
740 start_new_session = True 740 start_new_session = True
741 else: 741 else:
742 start_new_session = False 742 start_new_session = False
743 ensurestart = True 743 ensurestart = True
744 744
745 stdin = None
746
745 try: 747 try:
746 if stdin_bytes is None: 748 if stdin_bytes is None:
747 stdin = subprocess.DEVNULL 749 stdin = subprocess.DEVNULL
748 else: 750 else:
749 stdin = pycompat.unnamedtempfile() 751 stdin = pycompat.unnamedtempfile()
768 except Exception: 770 except Exception:
769 if record_wait is not None: 771 if record_wait is not None:
770 record_wait(255) 772 record_wait(255)
771 raise 773 raise
772 finally: 774 finally:
773 if stdin_bytes is not None: 775 if stdin_bytes is not None and stdin is not None:
774 assert not isinstance(stdin, int) 776 assert not isinstance(stdin, int)
775 stdin.close() 777 stdin.close()
776 if not ensurestart: 778 if not ensurestart:
777 # Even though we're not waiting on the child process, 779 # Even though we're not waiting on the child process,
778 # we still must call waitpid() on it at some point so 780 # we still must call waitpid() on it at some point so
850 % (cmd, os.strerror(returncode)), 852 % (cmd, os.strerror(returncode)),
851 ) 853 )
852 return 854 return
853 855
854 returncode = 255 856 returncode = 255
857 stdin = None
858
855 try: 859 try:
856 if record_wait is None: 860 if record_wait is None:
857 # Start a new session 861 # Start a new session
858 os.setsid() 862 os.setsid()
859 # connect stdin to devnull to make sure the subprocess can't 863 # connect stdin to devnull to make sure the subprocess can't
892 except Exception: 896 except Exception:
893 returncode = 255 897 returncode = 255
894 finally: 898 finally:
895 # mission accomplished, this child needs to exit and not 899 # mission accomplished, this child needs to exit and not
896 # continue the hg process here. 900 # continue the hg process here.
897 stdin.close() 901 if stdin is not None:
902 stdin.close()
898 if record_wait is None: 903 if record_wait is None:
899 os._exit(returncode) 904 os._exit(returncode)
900 905
901 if pycompat.ispy3: 906 if pycompat.ispy3:
902 # This branch is more robust, because it avoids running python 907 # This branch is more robust, because it avoids running python