Skip to content

Commit 0c51dc0

Browse files
committed
fix(args): better safety around sys.argv access
1 parent e74ec16 commit 0c51dc0

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

build_platform.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,22 @@
3535
boards_local_txt = None
3636
if "--boards-local-txt" in sys.argv:
3737
COPY_BOARDS_LOCAL_TXT = True
38-
if sys.argv.index("--boards-local-txt") + 1 >= len(sys.argv):
38+
if sys.argv.index("--boards-local-txt") + 1 == len(sys.argv):
3939
# check if in cwd
4040
if os.path.exists("boards.local.txt"):
4141
boards_local_txt = "boards.local.txt"
4242
else:
4343
sys.stderr.write("Error: --boards-local-txt option requires a path to boards.local.txt file or to be present in current directory\n")
4444
sys.exit(1)
4545
else:
46-
# get the boards.local.txt file from the command line
46+
# check second arg is file or another flag:
47+
if sys.argv[sys.argv.index("--boards-local-txt") + 1].startswith("--"):
48+
if os.path.exists("boards.local.txt"):
49+
boards_local_txt = "boards.local.txt"
50+
else:
51+
sys.stderr.write("Error: --boards-local-txt option requires a path to boards.local.txt file (or to be present in current directory)\n")
52+
sys.exit(1)
53+
# get the boards.local.txt file from the command line (index exists checked earlier)
4754
if not os.path.exists(sys.argv[sys.argv.index("--boards-local-txt") + 1]):
4855
sys.stderr.write("Error: boards.local.txt file does not exist\n")
4956
sys.exit(1)
@@ -482,7 +489,7 @@ def install_boards_local_txt(core_fqbn, boards_local_txt):
482489
config_output = subprocess.check_output(["arduino-cli", "config", "dump", "--format", "json"]).decode()
483490
config = json.loads(config_output)
484491
ColorPrint.print_info(f"Using arduino-cli config: {config_output.strip()}")
485-
492+
486493
# Extract data directory, with fallback to default
487494
data_dir = config.get("directories", {}).get("data", "")
488495
if not data_dir:
@@ -529,7 +536,7 @@ def install_boards_local_txt(core_fqbn, boards_local_txt):
529536
# Sort versions and take the latest (could be improved with proper version sorting)
530537
latest_version = sorted(versions)[-1]
531538
platform_path = os.path.join(platform_base, latest_version)
532-
539+
533540
dest_path = os.path.join(platform_path, "boards.local.txt")
534541
shutil.copyfile(boards_local_txt, dest_path)
535542
ColorPrint.print_info(f"Copied boards.local.txt to {dest_path}")
@@ -573,7 +580,7 @@ def main():
573580
if COPY_BOARDS_LOCAL_TXT and boards_local_txt:
574581
install_boards_local_txt(core_fqbn, boards_local_txt)
575582
print('#'*80)
576-
583+
577584
# Test examples in the platform folder
578585
if not IS_LEARNING_SYS:
579586
test_examples_in_folder(platform, BUILD_DIR+"/examples")

0 commit comments

Comments
 (0)