Skip to content

Commit 9e46ec0

Browse files
authored
When parsing settings as JSON, check the types we get back (#21206)
1 parent 876ae7c commit 9e46ec0

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

emcc.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def apply_user_settings():
275275
try:
276276
value = parse_value(value, expected_type)
277277
except Exception as e:
278-
exit_with_error('a problem occurred in evaluating the content after a "-s", specifically "%s=%s": %s', key, value, str(e))
278+
exit_with_error(f'error parsing "-s" setting "{key}={value}": {e}')
279279

280280
setattr(settings, user_key, value)
281281

@@ -1506,10 +1506,20 @@ def parse_string_list(text):
15061506
# if json parsing fails, we fall back to our own parser, which can handle a few
15071507
# simpler syntaxes
15081508
try:
1509-
return json.loads(text)
1509+
parsed = json.loads(text)
15101510
except ValueError:
15111511
return parse_string_list(text)
15121512

1513+
# if we succeeded in parsing as json, check some properties of it before returning
1514+
if type(parsed) not in (str, list):
1515+
raise ValueError(f'settings must be strings or lists (not ${type(parsed)})')
1516+
if type(parsed) is list:
1517+
for elem in parsed:
1518+
if type(elem) is not str:
1519+
raise ValueError(f'list members in settings must be strings (not ${type(elem)})')
1520+
1521+
return parsed
1522+
15131523
if expected_type == float:
15141524
try:
15151525
return float(text)

test/test_other.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -7660,7 +7660,7 @@ def test_dash_s_unclosed_list(self):
76607660

76617661
def test_dash_s_valid_list(self):
76627662
err = self.expect_fail([EMCC, test_file('hello_world.cpp'), "-sTEST_KEY=[Value1, \"Value2\"]"])
7663-
self.assertNotContained('a problem occurred in evaluating the content after a "-s", specifically', err)
7663+
self.assertNotContained('error parsing "-s" setting', err)
76647664

76657665
def test_dash_s_wrong_type(self):
76667666
err = self.expect_fail([EMCC, test_file('hello_world.cpp'), '-sEXIT_RUNTIME=[foo,bar]'])
@@ -7692,6 +7692,15 @@ def test_dash_s_hex(self):
76927692
# Ensure that 0x0 is parsed as a zero and not as the string '0x0'.
76937693
self.run_process([EMCC, test_file('hello_world.c'), '-nostdlib', '-sERROR_ON_UNDEFINED_SYMBOLS=0x0'])
76947694

7695+
def test_dash_s_bad_json_types(self):
7696+
# Dict rather than string/list
7697+
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sEXPORTED_FUNCTIONS={"a":1}'])
7698+
self.assertContained("settings must be strings or lists (not $<class 'dict'>", err)
7699+
7700+
# List element is not a string
7701+
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sEXPORTED_FUNCTIONS=[{"a":1}]'])
7702+
self.assertContained("list members in settings must be strings (not $<class 'dict'>)", err)
7703+
76957704
def test_zeroinit(self):
76967705
create_file('src.c', r'''
76977706
#include <stdio.h>

0 commit comments

Comments
 (0)