iniset: fix handling of values containg ampersand

Attempting to set a value containing the ampersand
character (&) by iniset would corrupt the value.
So, add an escaping process.

Signed-off-by: Nobuhiro MIKI <nmiki@yahoo-corp.jp>
Closes-Bug: #1983816
Change-Id: Ie2633bacd2d761d110e6cb12f95382325c329415
This commit is contained in:
Nobuhiro MIKI 2022-08-08 16:45:31 +09:00
parent 51a3f2ef1e
commit d266c87b1d
2 changed files with 14 additions and 1 deletions

View File

@ -189,6 +189,9 @@ function iniset {
local option=$3
local value=$4
# Escape the ampersand character (&)
value=$(echo $value | sed -e 's/&/\\&/g')
if [[ -z $section || -z $option ]]; then
$xtrace
return

View File

@ -44,6 +44,9 @@ empty =
multi = foo1
multi = foo2
[fff]
ampersand =
[key_with_spaces]
rgw special key = something
@ -85,7 +88,7 @@ fi
# test iniget_sections
VAL=$(iniget_sections "${TEST_INI}")
assert_equal "$VAL" "default aaa bbb ccc ddd eee key_with_spaces \
assert_equal "$VAL" "default aaa bbb ccc ddd eee fff key_with_spaces \
del_separate_options del_same_option del_missing_option \
del_missing_option_multi del_no_options"
@ -124,6 +127,13 @@ iniset ${SUDO_ARG} ${TEST_INI} bbb handlers "33,44"
VAL=$(iniget ${TEST_INI} bbb handlers)
assert_equal "$VAL" "33,44" "inset at EOF"
# Test with ampersand in values
for i in `seq 3`; do
iniset ${TEST_INI} fff ampersand '&y'
done
VAL=$(iniget ${TEST_INI} fff ampersand)
assert_equal "$VAL" "&y" "iniset ampersands in option"
# test empty option
if ini_has_option ${SUDO_ARG} ${TEST_INI} ddd empty; then
passed "ini_has_option: ddd.empty present"