From 612ccb72173cb8c86609d21b74db67cd4b14a0c5 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 5 Nov 2024 17:58:10 +0100 Subject: [PATCH] Use utf-8 when opening text files All of our source files are encoded in UTF-8. We have other scripts that make this assumption. We don't want to depend on the platform encoding. This fixes a bug whereby non-ASCII characters in .function files led to an error when writing the generated C file. Fix Mbed-TLS/mbedtls-framework#64. Signed-off-by: Gilles Peskine --- scripts/generate_test_code.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/generate_test_code.py b/scripts/generate_test_code.py index 2ac00add2..dea148da1 100755 --- a/scripts/generate_test_code.py +++ b/scripts/generate_test_code.py @@ -1092,8 +1092,8 @@ def read_code_from_input_files(platform_file, helpers_file, :return: """ # Read helpers - with open(helpers_file, 'r') as help_f, open(platform_file, 'r') as \ - platform_f: + with open(helpers_file, 'r', encoding='utf-8') as help_f, \ + open(platform_file, 'r', encoding='utf-8') as platform_f: snippets['test_common_helper_file'] = helpers_file snippets['test_common_helpers'] = help_f.read() snippets['test_platform_file'] = platform_file @@ -1123,7 +1123,8 @@ def write_test_source_file(template_file, c_file, snippets): invalid = "(?P__MBEDTLS_TEST_TEMPLATE__)" placeholder_pattern = re.compile("|".join([escaped, named, braced, invalid])) - with open(template_file, 'r') as template_f, open(c_file, 'w') as c_f: + with open(template_file, 'r', encoding='utf-8') as template_f, \ + open(c_file, 'w', encoding='utf-8') as c_f: for line_no, line in enumerate(template_f.readlines(), 1): # Update line number. +1 as #line directive sets next line number snippets['line_no'] = line_no + 1 @@ -1166,7 +1167,7 @@ def generate_intermediate_data_file(data_file, out_data_file, :return: """ with FileWrapper(data_file) as data_f, \ - open(out_data_file, 'w') as out_data_f: + open(out_data_file, 'w', encoding='utf-8') as out_data_f: dep_check_code, expression_code = gen_from_test_data( data_f, out_data_f, func_info, suite_dependencies) snippets['dep_check_code'] = dep_check_code