Skip to content

Commit bdaee66

Browse files
authored
Validate EXPORT_NAME (#14349)
Fixes: #11281
1 parent cecb106 commit bdaee66

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

emcc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def standardize_setting_change(key, value):
360360
filename = value[1:]
361361
if not os.path.exists(filename):
362362
exit_with_error('%s: file not found parsing argument: %s=%s' % (filename, key, value))
363-
value = open(filename).read()
363+
value = open(filename).read().strip()
364364
else:
365365
value = value.replace('\\', '\\\\')
366366

@@ -2231,6 +2231,9 @@ def get_full_import_name(name):
22312231
if settings.SINGLE_FILE:
22322232
exit_with_error('NODE_CODE_CACHING saves a file on the side and is not compatible with SINGLE_FILE')
22332233

2234+
if not shared.JS.isidentifier(settings.EXPORT_NAME):
2235+
exit_with_error(f'EXPORT_NAME is not a valid JS identifier: `{settings.EXPORT_NAME}`')
2236+
22342237
if options.tracing and settings.ALLOW_MEMORY_GROWTH:
22352238
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['emscripten_trace_report_memory_layout']
22362239
settings.EXPORTED_FUNCTIONS += ['_emscripten_stack_get_current',

tests/test_other.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10603,3 +10603,7 @@ def test_no_deprecated(self):
1060310603
err = self.expect_fail([EMCC, '-c', '-Werror', 'test.c'])
1060410604
self.assertContained("error: 'foo' is deprecated", err)
1060510605
self.run_process([EMCC, '-c', '-Werror', '-Wno-deprecated', 'test.c'])
10606+
10607+
def test_bad_export_name(self):
10608+
err = self.expect_fail([EMCC, '-sEXPORT_NAME=foo bar', test_file('hello_world.c')])
10609+
self.assertContained('error: EXPORT_NAME is not a valid JS identifier: `foo bar`', err)

tools/shared.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,11 @@ def is_legal_sig(sig):
678678
return True
679679
return sig == JS.legalize_sig(sig)
680680

681+
@staticmethod
682+
def isidentifier(name):
683+
# https://stackoverflow.com/questions/43244604/check-that-a-string-is-a-valid-javascript-identifier-name-using-python-3
684+
return name.replace('$', '_').isidentifier()
685+
681686
@staticmethod
682687
def make_dynCall(sig, args):
683688
# wasm2c and asyncify are not yet compatible with direct wasm table calls

0 commit comments

Comments
 (0)