From d266c87b1d3ecae1b40589832efc9bf7cf3e524c Mon Sep 17 00:00:00 2001 From: Nobuhiro MIKI Date: Mon, 8 Aug 2022 16:45:31 +0900 Subject: [PATCH] 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 Closes-Bug: #1983816 Change-Id: Ie2633bacd2d761d110e6cb12f95382325c329415 --- inc/ini-config | 3 +++ tests/test_ini_config.sh | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/inc/ini-config b/inc/ini-config index 79936823d2..f65e42d3a5 100644 --- a/inc/ini-config +++ b/inc/ini-config @@ -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 diff --git a/tests/test_ini_config.sh b/tests/test_ini_config.sh index 6ed1647f34..6367cde441 100755 --- a/tests/test_ini_config.sh +++ b/tests/test_ini_config.sh @@ -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"