Skip to content

Commit

Permalink
When parsing settings as JSON, check the types we get back
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Jan 29, 2024
1 parent 0e5b3a8 commit b483d9b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 13 additions & 2 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,9 @@ def apply_user_settings():
else:
try:
value = parse_value(value, expected_type)
print(value)
except Exception as e:
exit_with_error('a problem occurred in evaluating the content after a "-s", specifically "%s=%s": %s', key, value, str(e))
exit_with_error('error parsing "-s" setting "%s=%s": %s', key, value, str(e))

setattr(settings, user_key, value)

Expand Down Expand Up @@ -1506,10 +1507,20 @@ def parse_string_list(text):
# if json parsing fails, we fall back to our own parser, which can handle a few
# simpler syntaxes
try:
return json.loads(text)
parsed = json.loads(text)
except ValueError:
return parse_string_list(text)

# if we succeeded in parsing as json, check some properties of it before returning
if type(parsed) not in (str, list):
raise ValueError(f'setting must be parse as string or list (not ${type(parsed)})')
if type(parsed) is list:
for elem in parsed:
if type(elem) is not str:
raise ValueError(f'list members in settings must be strings (not ${type(elem)})')

return parsed

if expected_type == float:
try:
return float(text)
Expand Down
11 changes: 10 additions & 1 deletion test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -7651,7 +7651,7 @@ def test_dash_s_unclosed_list(self):

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

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

def test_dash_s_bad_json_types(self):
# Dict rather than string/list
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sEXPORTED_FUNCTIONS={"a":1}'])
self.assertContained("setting must be parse as string or list (not $<class 'dict'>", err)

# List element is not a string
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sEXPORTED_FUNCTIONS=[{"a":1}]'])
self.assertContained("list members in settings must be strings (not $<class 'dict'>)", err)

def test_zeroinit(self):
create_file('src.c', r'''
#include <stdio.h>
Expand Down

0 comments on commit b483d9b

Please sign in to comment.