From b483d9b932e3126cdf3dc8c9a9b9b4e2747ee4fc Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 29 Jan 2024 12:05:40 -0800 Subject: [PATCH] When parsing settings as JSON, check the types we get back --- emcc.py | 15 +++++++++++++-- test/test_other.py | 11 ++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/emcc.py b/emcc.py index 8870027348fcf..c60e2a8dad9a5 100644 --- a/emcc.py +++ b/emcc.py @@ -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) @@ -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) diff --git a/test/test_other.py b/test/test_other.py index 7191bc1120e82..9f3470ea210b4 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -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]']) @@ -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 $", 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 $)", err) + def test_zeroinit(self): create_file('src.c', r''' #include