From 3f58831acc31eb33245b2dd025537c8fbe1f1d2b Mon Sep 17 00:00:00 2001 From: Joar Wandborg Date: Tue, 26 Nov 2024 17:01:59 +0100 Subject: [PATCH] printEnvVariablesToFile.py overwrites itself if invoked without arguments (#24487) I saw the command in my terminal when launching the editor, was curious to see what it did, so I ran it again without arguments, expecting to get a `--help`-like message, instead it ended up overwriting itself. This change changes the script's default behavior (default being the invocation without arguments) from overwriting itself to throwing an exception about a missing output file argument. --- python_files/printEnvVariablesToFile.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python_files/printEnvVariablesToFile.py b/python_files/printEnvVariablesToFile.py index c7ec70dd9684..eae01b3d073c 100644 --- a/python_files/printEnvVariablesToFile.py +++ b/python_files/printEnvVariablesToFile.py @@ -4,8 +4,12 @@ import os import sys -# Last argument is the target file into which we'll write the env variables line by line. -output_file = sys.argv[-1] +# Prevent overwriting itself, since sys.argv[0] is the path to this file +if len(sys.argv) > 1: + # Last argument is the target file into which we'll write the env variables line by line. + output_file = sys.argv[-1] +else: + raise ValueError("Missing output file argument") with open(output_file, "w") as outfile: # noqa: PTH123 for key, val in os.environ.items():