Skip to content

Commit

Permalink
Fix crash on missing linker flags
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Jan 21, 2025
1 parent 19bcea4 commit 76ff21f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 8 additions & 3 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = ''
Expand All @@ -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='):
Expand All @@ -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 == '-':
Expand Down Expand Up @@ -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] = ''
Expand Down
4 changes: 4 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 76ff21f

Please sign in to comment.