From 83252158e43e5b3398ccb23869ce7ddaebcc1be3 Mon Sep 17 00:00:00 2001 From: HuijingHei Date: Wed, 20 Dec 2023 11:00:54 +0800 Subject: [PATCH] kargs: fix corner case when `append` and `append-if-missing` with the same value which will append twice Consider a safe way to fix `corner case where --append foo --append-if-missing foo would append foo twice`. --- src/daemon/rpmostreed-transaction-types.cxx | 3 ++- tests/vmcheck/test-kernel-args.sh | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 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 aa952022c1..f64b95b5d5 100755 --- a/tests/vmcheck/test-kernel-args.sh +++ b/tests/vmcheck/test-kernel-args.sh @@ -187,7 +187,18 @@ vm_rpmostree kargs > kargs.txt assert_file_has_content_literal kargs.txt 'PACKAGE3=NEWTEST' rpm-ostree kargs --delete-if-present=PACKAGE3=TEST || rc=$? assert_streq $rc 0 -vm_rpmostree kargs --delete-if-present=PACKAGE3=TEST3 +echo "ok for append-if-missing and delete-if-present if 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=PACKAGE4=TEST4 --append-if-missing=PACKAGE4=TEST4 +vm_rpmostree kargs > kargs.txt +assert_not_file_has_content_literal kargs.txt 'PACKAGE4=TEST4 PACKAGE4=TEST4' +assert_file_has_content_literal kargs.txt 'PACKAGE4=TEST4' +echo "ok for append and append-if-missing with the same value" # Test for 'rpm-ostree kargs --editor'. vm_rpmostree kargs > kargs.txt