Skip to content

Commit 797fcd9

Browse files
authored
Fix crash on missing linker flags (emscripten-core#23461)
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 ```
1 parent 19bcea4 commit 797fcd9

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

emcc.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,11 @@ def phase_setup(options, state, newargs):
779779
if arg in CLANG_FLAGS_WITH_ARGS:
780780
skip = True
781781

782+
def get_next_arg():
783+
if len(newargs) <= i + 1:
784+
exit_with_error(f"option '{arg}' requires an argument")
785+
return newargs[i + 1]
786+
782787
if not arg.startswith('-'):
783788
# we already removed -o <target>, so all these should be inputs
784789
newargs[i] = ''
@@ -797,7 +802,7 @@ def phase_setup(options, state, newargs):
797802
state.add_link_flag(i, arg)
798803
elif arg == '-z':
799804
state.add_link_flag(i, newargs[i])
800-
state.add_link_flag(i + 1, newargs[i + 1])
805+
state.add_link_flag(i + 1, get_next_arg())
801806
elif arg.startswith('-z'):
802807
state.add_link_flag(i, newargs[i])
803808
elif arg.startswith('--js-library='):
@@ -810,7 +815,7 @@ def phase_setup(options, state, newargs):
810815
for flag_index, flag in enumerate(link_flags_to_add):
811816
state.add_link_flag(i + float(flag_index) / len(link_flags_to_add), flag)
812817
elif arg == '-Xlinker':
813-
state.add_link_flag(i + 1, newargs[i + 1])
818+
state.add_link_flag(i + 1, get_next_arg())
814819
elif arg == '-s':
815820
state.add_link_flag(i, newargs[i])
816821
elif arg == '-':
@@ -1154,7 +1159,7 @@ def check_arg(name):
11541159
return True
11551160
if arg == name:
11561161
if len(newargs) <= i + 1:
1157-
exit_with_error("option '%s' requires an argument" % arg)
1162+
exit_with_error(f"option '{arg}' requires an argument")
11581163
arg_value = newargs[i + 1]
11591164
newargs[i] = ''
11601165
newargs[i + 1] = ''

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ lint.ignore = [
5050
"PLW2901",
5151
]
5252
lint.per-file-ignores."emrun.py" = [ "PLE0704" ]
53-
lint.mccabe.max-complexity = 49 # Recommended: 10
53+
lint.mccabe.max-complexity = 51 # Recommended: 10
5454
lint.pylint.allow-magic-value-types = [
5555
"bytes",
5656
"float",

test/test_other.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12300,6 +12300,10 @@ def test_linker_flags_unused(self):
1230012300
err = self.run_process([EMCC, test_file('hello_world.c'), '-Wl,-static', '-Xlinker', '-static'], stderr=PIPE).stderr
1230112301
self.assertNotContained("input unused", err)
1230212302

12303+
def test_linker_flags_missing(self):
12304+
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-Xlinker'])
12305+
self.assertContained("emcc: error: option '-Xlinker' requires an argument", err)
12306+
1230312307
def test_linker_input_unused(self):
1230412308
self.run_process([EMCC, '-c', test_file('hello_world.c')])
1230512309
err = self.run_process([EMCC, 'hello_world.o', '-c', '-o', 'out.o'], stderr=PIPE).stderr

0 commit comments

Comments
 (0)