[PATCH 1/2] scripts/config: preserve ampersands in string values

From: Daniel Syauqi

Date: Mon Nov 30 2026 - 02:53:06 EST


Setting CMDLINE to 'a=1&b=2' with --set-str works when the symbol is
absent. When replacing an existing assignment, sed expands the
ampersand to the entire matched line. The command succeeds but writes
a different kernel command line. Embedded quotes from the old value
can also truncate the string on the next olddefconfig.

Escape backslashes and quotes when forming the Kconfig string and
escape backslashes and ampersands when forming the sed replacement.
This lets insertion and replacement write the same value, including
when a backslash precedes an ampersand. Decode the escaped backslashes
when reading the value back with --state.

Fixes: 1f990cf94559 ("kbuild: add generic --set-str option to scripts/config")
Signed-off-by: Daniel Syauqi <syauqicode@xxxxxxxxx>
---
scripts/config | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/scripts/config b/scripts/config
index 32428ea90..ba66b681d 100755
--- a/scripts/config
+++ b/scripts/config
@@ -86,9 +86,10 @@ txt_append() {

txt_subst() {
local before="$1"
- local after="$2"
+ local after="${2//\\/\\\\}"
local infile="$3"

+ after="${after//&/\\&}"
sed -E -i.swp -e "s$SED_DELIM$before$SED_DELIM$after$SED_DELIM" "$infile"
SED_EDITED=1
}
@@ -186,8 +187,9 @@ while [ "$1" != "" ] ; do
;;

--set-str)
- # sed swallows one level of escaping, so we need double-escaping
- set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${1//\"/\\\\\"}\""
+ # Escape backslashes and double quotes in the Kconfig string.
+ value="${1//\\/\\\\}"
+ set_var "${CONFIG_}$ARG" "${CONFIG_}$ARG=\"${value//\"/\\\"}\""
shift
;;

@@ -211,6 +213,7 @@ while [ "$1" != "" ] ; do
V="${V/#\"/}"
V="${V/%\"/}"
V="${V//\\\"/\"}"
+ V="${V//\\\\/\\}"
echo "${V}"
fi
fi