Skip to content

Commit

Permalink
Fix crash on missing linker flags (#23461)
Browse files Browse the repository at this point in the history
Without this change running `emcc test/hello_world.c -Xlinker` will
crash with:

```
...
  File "emcc.py", line 813, in phase_setup
    state.add_link_flag(i + 1, newargs[i + 1])
                               ~~~~~~~^^^^^^^
IndexError: list index out of range
```
sbc100 authored Jan 21, 2025
1 parent 19bcea4 commit 797fcd9
Showing 3 changed files with 13 additions and 4 deletions.
11 changes: 8 additions & 3 deletions emcc.py
Original file line number Diff line number Diff line change
@@ -779,6 +779,11 @@ def phase_setup(options, state, newargs):
if arg in CLANG_FLAGS_WITH_ARGS:
skip = True

def get_next_arg():
if len(newargs) <= i + 1:
exit_with_error(f"option '{arg}' requires an argument")
return newargs[i + 1]

if not arg.startswith('-'):
# we already removed -o <target>, so all these should be inputs
newargs[i] = ''
@@ -797,7 +802,7 @@ def phase_setup(options, state, newargs):
state.add_link_flag(i, arg)
elif arg == '-z':
state.add_link_flag(i, newargs[i])
state.add_link_flag(i + 1, newargs[i + 1])
state.add_link_flag(i + 1, get_next_arg())
elif arg.startswith('-z'):
state.add_link_flag(i, newargs[i])
elif arg.startswith('--js-library='):
@@ -810,7 +815,7 @@ def phase_setup(options, state, newargs):
for flag_index, flag in enumerate(link_flags_to_add):
state.add_link_flag(i + float(flag_index) / len(link_flags_to_add), flag)
elif arg == '-Xlinker':
state.add_link_flag(i + 1, newargs[i + 1])
state.add_link_flag(i + 1, get_next_arg())
elif arg == '-s':
state.add_link_flag(i, newargs[i])
elif arg == '-':
@@ -1154,7 +1159,7 @@ def check_arg(name):
return True
if arg == name:
if len(newargs) <= i + 1:
exit_with_error("option '%s' requires an argument" % arg)
exit_with_error(f"option '{arg}' requires an argument")
arg_value = newargs[i + 1]
newargs[i] = ''
newargs[i + 1] = ''
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ lint.ignore = [
"PLW2901",
]
lint.per-file-ignores."emrun.py" = [ "PLE0704" ]
lint.mccabe.max-complexity = 49 # Recommended: 10
lint.mccabe.max-complexity = 51 # Recommended: 10
lint.pylint.allow-magic-value-types = [
"bytes",
"float",
4 changes: 4 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
@@ -12300,6 +12300,10 @@ def test_linker_flags_unused(self):
err = self.run_process([EMCC, test_file('hello_world.c'), '-Wl,-static', '-Xlinker', '-static'], stderr=PIPE).stderr
self.assertNotContained("input unused", err)

def test_linker_flags_missing(self):
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-Xlinker'])
self.assertContained("emcc: error: option '-Xlinker' requires an argument", err)

def test_linker_input_unused(self):
self.run_process([EMCC, '-c', test_file('hello_world.c')])
err = self.run_process([EMCC, 'hello_world.o', '-c', '-o', 'out.o'], stderr=PIPE).stderr

0 comments on commit 797fcd9

Please sign in to comment.