From b5aa8c344acf36a9baf5eedcf4cba703771572a8 Mon Sep 17 00:00:00 2001 From: HuijingHei Date: Wed, 20 Dec 2023 14:32:15 +0800 Subject: [PATCH] kargs: fix corner case when `append` and `append-if-missing` with the same value which will append twice --- src/daemon/rpmostreed-transaction-types.cxx | 3 ++- tests/vmcheck/test-kernel-args.sh | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/daemon/rpmostreed-transaction-types.cxx b/src/daemon/rpmostreed-transaction-types.cxx index 033a97a4bc..9067b0a02b 100644 --- a/src/daemon/rpmostreed-transaction-types.cxx +++ b/src/daemon/rpmostreed-transaction-types.cxx @@ -2708,7 +2708,8 @@ kernel_arg_apply_patching (KernelArgTransaction *self, RpmOstreeSysrootUpgrader for (char **iter = append_if_missing; iter && *iter; iter++) { const char *arg = *iter; - if (!g_strv_contains (existing_kargs, arg)) + g_auto (GStrv) kargs_strv = ostree_kernel_args_to_strv (kargs); + if (!g_strv_contains (kargs_strv, arg)) { ostree_kernel_args_append (kargs, arg); changed = TRUE; diff --git a/tests/vmcheck/test-kernel-args.sh b/tests/vmcheck/test-kernel-args.sh index 8c62d777bc..e66373cfc6 100755 --- a/tests/vmcheck/test-kernel-args.sh +++ b/tests/vmcheck/test-kernel-args.sh @@ -182,13 +182,24 @@ echo "ok exit 77 when unchanged kargs with unchanged-exit-77" vm_rpmostree kargs --append-if-missing=PACKAGE4=TEST4 vm_rpmostree kargs > kargs.txt assert_file_has_content_literal kargs.txt 'PACKAGE4=TEST4' -rpm-ostree kargs --append-if-missing=PACKAGE4=NEWTEST +vm_rpmostree kargs --append-if-missing=PACKAGE4=NEWTEST vm_rpmostree kargs > kargs.txt assert_file_has_content_literal kargs.txt 'PACKAGE4=NEWTEST' -rpm-ostree kargs --delete-if-present=PACKAGE4=TEST || rc=$? -assert_streq $rc 0 +vm_rpmostree kargs --delete-if-present=PACKAGE4=TEST --unchanged-exit-77 || rc=$? +assert_streq $rc 77 echo "ok for append-if-missing and delete-if-present with existing key" +# Test corner case for append and append-if-missing with the same value +vm_rpmostree kargs --append=foo --append-if-missing=foo +vm_rpmostree kargs > kargs.txt +assert_not_file_has_content_literal kargs.txt 'foo foo' +assert_file_has_content_literal kargs.txt 'foo' +vm_rpmostree kargs --append-if-missing=bar=foo --append-if-missing=bar=foo +vm_rpmostree kargs > kargs.txt +assert_not_file_has_content_literal kargs.txt 'bar=foo bar=foo' +assert_file_has_content_literal kargs.txt 'bar=foo' +echo "ok for append and append-if-missing with the same value" + # Test for 'rpm-ostree kargs --editor'. vm_rpmostree kargs > kargs.txt assert_not_file_has_content_literal kargs.txt 'nonexisting'