Fix iniset to escape backslash characters

This patch fixes an issue in iniset where backslash (\) characters are
removed from the config value.

This patch ensures that backslash characters (\) are escaped in addition
to the ampersand (&) character that was already being escaped.

Closes-Bug: #2120180
Signed-off-by: Douglas Mendizábal <dmendiza@redhat.com>
Change-Id: Ica53ed42269931d151daf815d2e2c10c1f9e29a8
This commit is contained in:
Douglas Mendizabal
2025-08-08 10:44:41 -04:00
committed by Douglas Mendizábal
parent 3d013ef97f
commit 1a74605eb4
2 changed files with 18 additions and 3 deletions

View File

@@ -189,8 +189,10 @@ function iniset {
local option=$3
local value=$4
# Escape the ampersand character (&)
value=$(echo $value | sed -e 's/&/\\&/g')
# Escape the ampersand (&) and backslash (\) characters for sed
# Order of substitution matters: we escape backslashes first before
# adding more backslashes to escape ampersands
value=$(echo $value | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g')
if [[ -z $section || -z $option ]]; then
$xtrace

View File

@@ -47,6 +47,9 @@ multi = foo2
[fff]
ampersand =
[ggg]
backslash =
[key_with_spaces]
rgw special key = something
@@ -88,7 +91,7 @@ fi
# test iniget_sections
VAL=$(iniget_sections "${TEST_INI}")
assert_equal "$VAL" "default aaa bbb ccc ddd eee fff key_with_spaces \
assert_equal "$VAL" "default aaa bbb ccc ddd eee fff ggg key_with_spaces \
del_separate_options del_same_option del_missing_option \
del_missing_option_multi del_no_options"
@@ -134,6 +137,16 @@ done
VAL=$(iniget ${TEST_INI} fff ampersand)
assert_equal "$VAL" "&y" "iniset ampersands in option"
# Test with backslash in value
iniset ${TEST_INI} ggg backslash 'foo\bar'
VAL=$(iniget ${TEST_INI} ggg backslash)
assert_equal "$VAL" 'foo\bar' "iniset backslash in value"
# Test with both ampersand and backslash
iniset ${TEST_INI} ggg backslash 'foo\bar&baz'
VAL=$(iniget ${TEST_INI} ggg backslash)
assert_equal "$VAL" 'foo\bar&baz' "iniset ampersand and backslash in value"
# test empty option
if ini_has_option ${SUDO_ARG} ${TEST_INI} ddd empty; then
passed "ini_has_option: ddd.empty present"