Skip to content

Commit f3c0afb

Browse files
committed
[nrf fromtree] cmake: escape json input string
Escape the json input string given to `to_yaml()` function and the content given to `yaml_set()` function. This ensures that a string like `foo\bar` becomes `foo\\bar` during internal CMake json processing and when written to the file it becomes `foo\bar`. Signed-off-by: Torsten Rasmussen <[email protected]> (cherry picked from commit cdbe424)
1 parent 69a40de commit f3c0afb

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

cmake/modules/extensions.cmake

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3073,6 +3073,16 @@ endfunction()
30733073
# This function extends the CMake string function by providing additional
30743074
# manipulation arguments to CMake string.
30753075
#
3076+
# ESCAPE: Ensure that any single '\' in the input string is escaped with the
3077+
# escape char '\'. For example the string 'foo\bar' will be escaped
3078+
# so that it becomes 'foo\\bar'.
3079+
# Backslashes which are already escaped will not be escaped further,
3080+
# for example 'foo\\bar' will not be modified.
3081+
# This is useful for handling of windows path separator in strings or
3082+
# when strings contains newline escapes such as '\n' and this can
3083+
# cause issues when writing to a file where a '\n' is desired in the
3084+
# string instead of a newline.
3085+
#
30763086
# SANITIZE: Ensure that the output string does not contain any special
30773087
# characters. Special characters, such as -, +, =, $, etc. are
30783088
# converted to underscores '_'.
@@ -3084,9 +3094,11 @@ endfunction()
30843094
#
30853095
# returns the updated string
30863096
function(zephyr_string)
3087-
set(options SANITIZE TOUPPER)
3097+
set(options SANITIZE TOUPPER ESCAPE)
30883098
cmake_parse_arguments(ZEPHYR_STRING "${options}" "" "" ${ARGN})
30893099

3100+
zephyr_check_flags_exclusive(${CMAKE_CURRENT_FUNCTION} ZEPHYR_STRING SANITIZE ESCAPE)
3101+
30903102
if (NOT ZEPHYR_STRING_UNPARSED_ARGUMENTS)
30913103
message(FATAL_ERROR "Function zephyr_string() called without a return variable")
30923104
endif()
@@ -3104,6 +3116,13 @@ function(zephyr_string)
31043116
string(TOUPPER ${work_string} work_string)
31053117
endif()
31063118

3119+
if(ZEPHYR_STRING_ESCAPE)
3120+
# If a single '\' is discovered, such as 'foo\bar', then it must be escaped like: 'foo\\bar'
3121+
# \\1 and \\2 are keeping the match patterns, the \\\\ --> \\ meaning an escaped '\',
3122+
# which then becomes a single '\' in the final string.
3123+
string(REGEX REPLACE "([^\\][\\])([^\\])" "\\1\\\\\\2" work_string "${ZEPHYR_STRING_UNPARSED_ARGUMENTS}")
3124+
endif()
3125+
31073126
set(${return_arg} ${work_string} PARENT_SCOPE)
31083127
endfunction()
31093128

cmake/modules/yaml.cmake

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,11 @@ function(yaml_set)
422422
internal_yaml_list_initializer(json_string ${genex})
423423
string(JSON json_content SET "${json_content}" ${ARG_YAML_KEY} "${json_string}")
424424
endif()
425-
internal_yaml_list_append(json_content ${genex} "${ARG_YAML_KEY}" ${ARG_YAML_LIST})
425+
zephyr_string(ESCAPE escape_list "${ARG_YAML_LIST}")
426+
internal_yaml_list_append(json_content ${genex} "${ARG_YAML_KEY}" ${escape_list})
426427
else()
427-
string(JSON json_content SET "${json_content}" ${ARG_YAML_KEY} "\"${ARG_YAML_VALUE}\"")
428+
zephyr_string(ESCAPE escape_value "${ARG_YAML_VALUE}")
429+
string(JSON json_content SET "${json_content}" ${ARG_YAML_KEY} "\"${escape_value}\"")
428430
endif()
429431

430432
zephyr_set(JSON "${json_content}" SCOPE ${ARG_YAML_NAME})
@@ -539,7 +541,8 @@ function(yaml_save)
539541
endif()
540542
endfunction()
541543

542-
function(to_yaml json level yaml genex)
544+
function(to_yaml in_json level yaml genex)
545+
zephyr_string(ESCAPE json "${in_json}")
543546
if(level GREATER 0)
544547
math(EXPR level_dec "${level} - 1")
545548
set(indent_${level} "${indent_${level_dec}} ")

0 commit comments

Comments
 (0)