Skip to content

Commit 6edefd8

Browse files
joelspadinmmahadevan108
authored andcommitted
scripts: dts: properly escape string properties
Fixed escaping of double quotes, backslashes, and new line characters so they can be used in string properties. Previously, double quotes and backslashes were escaped in gen_defines.py but not in gen_dts_cmake.py, and new lines were not escaped in either, so using any of these characters would break the build. Signed-off-by: Joel Spadin <[email protected]>
1 parent 0515bff commit 6edefd8

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

scripts/dts/gen_defines.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ def write_vanilla_props(node: edtlib.Node) -> None:
583583

584584
if prop.spec.type == 'string':
585585
# DT_N_<node-id>_P_<prop-id>_IDX_<i>_STRING_UNQUOTED
586-
macro2val[macro + "_STRING_UNQUOTED"] = prop.val
586+
macro2val[macro + "_STRING_UNQUOTED"] = escape_unquoted(prop.val)
587587
# DT_N_<node-id>_P_<prop-id>_IDX_<i>_STRING_TOKEN
588588
macro2val[macro + "_STRING_TOKEN"] = prop.val_as_token
589589
# DT_N_<node-id>_P_<prop-id>_IDX_<i>_STRING_UPPER_TOKEN
@@ -626,7 +626,7 @@ def write_vanilla_props(node: edtlib.Node) -> None:
626626
macro2val[macro + f"_IDX_{i}"] = quote_str(subval)
627627
subval_as_token = edtlib.str_as_token(subval)
628628
# DT_N_<node-id>_P_<prop-id>_IDX_<i>_STRING_UNQUOTED
629-
macro2val[macro + f"_IDX_{i}_STRING_UNQUOTED"] = subval
629+
macro2val[macro + f"_IDX_{i}_STRING_UNQUOTED"] = escape_unquoted(subval)
630630
# DT_N_<node-id>_P_<prop-id>_IDX_<i>_STRING_TOKEN
631631
macro2val[macro + f"_IDX_{i}_STRING_TOKEN"] = subval_as_token
632632
# DT_N_<node-id>_P_<prop-id>_IDX_<i>_STRING_UPPER_TOKEN
@@ -1020,11 +1020,20 @@ def out_comment(s: str, blank_before=True) -> None:
10201020
print("/* " + s + " */", file=header_file)
10211021

10221022

1023+
ESCAPE_TABLE = str.maketrans(
1024+
{
1025+
"\n": "\\n",
1026+
"\r": "\\r",
1027+
"\\": "\\\\",
1028+
'"': '\\"',
1029+
}
1030+
)
1031+
1032+
10231033
def escape(s: str) -> str:
1024-
# Backslash-escapes any double quotes and backslashes in 's'
1034+
# Backslash-escapes any double quotes, backslashes, and new lines in 's'
10251035

1026-
# \ must be escaped before " to avoid double escaping
1027-
return s.replace("\\", "\\\\").replace('"', '\\"')
1036+
return s.translate(ESCAPE_TABLE)
10281037

10291038

10301039
def quote_str(s: str) -> str:
@@ -1034,6 +1043,15 @@ def quote_str(s: str) -> str:
10341043
return f'"{escape(s)}"'
10351044

10361045

1046+
def escape_unquoted(s: str) -> str:
1047+
# C macros cannot contain line breaks, so replace them with spaces.
1048+
# Whitespace is used to separate preprocessor tokens, but it does not matter
1049+
# which whitespace characters are used, so a line break and a space are
1050+
# equivalent with regards to unquoted strings being used as C code.
1051+
1052+
return s.replace("\r", " ").replace("\n", " ")
1053+
1054+
10371055
def err(s: str) -> NoReturn:
10381056
raise Exception(s)
10391057

scripts/dts/gen_dts_cmake.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@
4848
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'python-devicetree',
4949
'src'))
5050

51+
ESCAPE_TABLE = str.maketrans(
52+
{
53+
"\n": "\\n",
54+
"\r": "\\r",
55+
'\"': '\\"',
56+
"\\": "\\\\",
57+
}
58+
)
59+
60+
61+
def escape(value):
62+
if isinstance(value, str):
63+
return value.translate(ESCAPE_TABLE)
64+
65+
return value
66+
5167

5268
def parse_args():
5369
# Returns parsed command-line arguments
@@ -117,7 +133,7 @@ def main():
117133
# Encode node's property 'item' as a CMake target property
118134
# with a name like 'DT_PROP|<path>|<property>'.
119135
cmake_prop = f'DT_PROP|{node.path}|{item}'
120-
cmake_props.append(f'"{cmake_prop}" "{cmake_value}"')
136+
cmake_props.append(f'"{cmake_prop}" "{escape(cmake_value)}"')
121137

122138
if item == 'compatible':
123139
# compatibles is always an array

0 commit comments

Comments
 (0)