Skip to content

Commit b483d9b

Browse files
committed
When parsing settings as JSON, check the types we get back
1 parent 0e5b3a8 commit b483d9b

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

emcc.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,9 @@ def apply_user_settings():
274274
else:
275275
try:
276276
value = parse_value(value, expected_type)
277+
print(value)
277278
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))
279+
exit_with_error('error parsing "-s" setting "%s=%s": %s', key, value, str(e))
279280

280281
setattr(settings, user_key, value)
281282

@@ -1506,10 +1507,20 @@ def parse_string_list(text):
15061507
# if json parsing fails, we fall back to our own parser, which can handle a few
15071508
# simpler syntaxes
15081509
try:
1509-
return json.loads(text)
1510+
parsed = json.loads(text)
15101511
except ValueError:
15111512
return parse_string_list(text)
15121513

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

test/test_other.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7651,7 +7651,7 @@ def test_dash_s_unclosed_list(self):
76517651

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

76567656
def test_dash_s_wrong_type(self):
76577657
err = self.expect_fail([EMCC, test_file('hello_world.cpp'), '-sEXIT_RUNTIME=[foo,bar]'])
@@ -7683,6 +7683,15 @@ def test_dash_s_hex(self):
76837683
# Ensure that 0x0 is parsed as a zero and not as the string '0x0'.
76847684
self.run_process([EMCC, test_file('hello_world.c'), '-nostdlib', '-sERROR_ON_UNDEFINED_SYMBOLS=0x0'])
76857685

7686+
def test_dash_s_bad_json_types(self):
7687+
# Dict rather than string/list
7688+
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sEXPORTED_FUNCTIONS={"a":1}'])
7689+
self.assertContained("setting must be parse as string or list (not $<class 'dict'>", err)
7690+
7691+
# List element is not a string
7692+
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sEXPORTED_FUNCTIONS=[{"a":1}]'])
7693+
self.assertContained("list members in settings must be strings (not $<class 'dict'>)", err)
7694+
76867695
def test_zeroinit(self):
76877696
create_file('src.c', r'''
76887697
#include <stdio.h>

0 commit comments

Comments
 (0)