Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When parsing settings as JSON, check the types we get back #21206

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def apply_user_settings():
try:
value = parse_value(value, expected_type)
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(f'error parsing "-s" setting "{key}={value}": {e}')

setattr(settings, user_key, value)

Expand Down Expand Up @@ -1506,10 +1506,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'settings must be strings or lists (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 @@ -7660,7 +7660,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 @@ -7692,6 +7692,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("settings must be strings or lists (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
Loading