]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: use iovec_equal()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 18 Apr 2026 18:30:49 +0000 (03:30 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 18 Apr 2026 22:31:07 +0000 (07:31 +0900)
14 files changed:
coccinelle/memcmp.cocci [new file with mode: 0644]
src/core/namespace.c
src/cryptenroll/cryptenroll-tpm2.c
src/import/pull-job.c
src/import/pull-oci.c
src/shared/dissect-image.c
src/shared/tpm2-util.c
src/sysupdate/sysupdate-resource.c
src/test/test-creds.c
src/test/test-fileio.c
src/test/test-iovec-wrapper.c
src/test/test-json.c
src/test/test-log.c
src/test/test-tpm2.c

diff --git a/coccinelle/memcmp.cocci b/coccinelle/memcmp.cocci
new file mode 100644 (file)
index 0000000..aa0effa
--- /dev/null
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/* Disable this transformation for iovec-util.h and the unit test */
+@ depends on !(file in "src/basic/iovec-util.h")
+            && !(file in "src/test/test-iovec-util.c") @
+expression a, b;
+@@
+(
+- iovec_memcmp(a, b) == 0
++ iovec_equal(a, b)
+|
+- iovec_memcmp(a, b) != 0
++ !iovec_equal(a, b)
+|
+- ASSERT_EQ(iovec_memcmp(a, b), 0)
++ ASSERT_TRUE(iovec_equal(a, b))
+|
+- ASSERT_NE(iovec_memcmp(a, b), 0)
++ ASSERT_FALSE(iovec_equal(a, b))
+)
index ff4592b8b90cdc3b51c008218f4e119a544f0f95..bd27cbe2b5c703603723596baa1a3ba29c936fd0 100644 (file)
@@ -1049,7 +1049,7 @@ static bool verity_has_later_duplicates(MountList *ml, const MountEntry *needle)
         for (const MountEntry *m = needle + 1; m < ml->mounts + ml->n_mounts; m++) {
                 if (m->mode != MOUNT_EXTENSION_IMAGE)
                         continue;
-                if (iovec_memcmp(&m->verity.root_hash, &needle->verity.root_hash) == 0)
+                if (iovec_equal(&m->verity.root_hash, &needle->verity.root_hash))
                         return true;
         }
 
index 50abca43639b8023cbf15c07757c90e5192925bf..9badd9fd6760fb4ed0015ac4d2be69096c82127c 100644 (file)
@@ -72,7 +72,7 @@ static int search_policy_hash(
                                         if (r < 0)
                                                 return log_error_errno(r, "Invalid hex data in 'tpm2-policy-hash' field item : %m");
 
-                                        if (iovec_memcmp(policy_hash + j, &thash) != 0) {
+                                        if (!iovec_equal(policy_hash + j, &thash)) {
                                                 match = false;
                                                 break;
                                         }
@@ -91,7 +91,7 @@ static int search_policy_hash(
                         if (r < 0)
                                 return log_error_errno(r, "Invalid hex data in 'tpm2-policy-hash' field: %m");
 
-                        if (iovec_memcmp(policy_hash + 0, &thash) == 0)
+                        if (iovec_equal(policy_hash + 0, &thash))
                                 return keyslot; /* Found entry with same hash. */
                 }
         }
@@ -566,7 +566,7 @@ int enroll_tpm2(struct crypt_device *cd,
                 if (r < 0)
                         return log_error_errno(r, "Failed to unseal secret using TPM2: %m");
 
-                if (iovec_memcmp(&secret, &secret2) != 0)
+                if (!iovec_equal(&secret, &secret2))
                         return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "TPM2 seal/unseal verification failed.");
         }
 
index 385043dda8003fe54012aece18ba663f0b1c6e12..e847d5e2fbe419fed275ad32ffbaed0e9e10331f 100644 (file)
@@ -298,7 +298,7 @@ void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) {
                 }
 
                 if (iovec_is_set(&j->expected_checksum) &&
-                    iovec_memcmp(&j->checksum, &j->expected_checksum) != 0) {
+                    !iovec_equal(&j->checksum, &j->expected_checksum)) {
                         r = log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Checksum of downloaded resource does not match expected checksum, yikes.");
                         goto finish;
                 }
index 4ae933928ff5f9d55631f6ed92e685e4eaff9370..f4878e3c87d31f81de220008b98ad7561b0bf105 100644 (file)
@@ -1195,7 +1195,7 @@ static int oci_pull_make_local(OciPull *i) {
                 return r;
 
         if (!iovec_is_set(&i->config) ||
-            iovec_memcmp(&i->config, &IOVEC_MAKE_STRING("{}")) == 0)
+            iovec_equal(&i->config, &IOVEC_MAKE_STRING("{}")))
                 log_info("Image has no configuration, not saving.");
         else {
                 r = oci_pull_save_nspawn_settings(i);
index e33e78d31026dc589d40313bffa5deb14288de68..894fabf7a6eeebbcc1e780195bb5a9b230b21eb1 100644 (file)
@@ -1422,7 +1422,7 @@ static int dissect_image(
                                                         /* ret_root_hash_sig= */ NULL);
                                         if (r < 0)
                                                 return r;
-                                        if (iovec_memcmp(&verity->root_hash, &root_hash) != 0) {
+                                        if (!iovec_equal(&verity->root_hash, &root_hash)) {
                                                 if (DEBUG_LOGGING) {
                                                         _cleanup_free_ char *found = NULL, *expected = NULL;
 
@@ -3023,7 +3023,7 @@ static int verity_can_reuse(
         r = sym_crypt_volume_key_get(cd, CRYPT_ANY_SLOT, root_hash_existing.iov_base, &root_hash_existing.iov_len, NULL, 0);
         if (r < 0)
                 return log_debug_errno(r, "Error opening verity device, crypt_volume_key_get failed: %m");
-        if (iovec_memcmp(&verity->root_hash, &root_hash_existing) != 0)
+        if (!iovec_equal(&verity->root_hash, &root_hash_existing))
                 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Error opening verity device, it already exists but root hashes are different.");
 
         /* Ensure that, if signatures are supported, we only reuse the device if the previous mount used the
@@ -4048,7 +4048,7 @@ int dissected_image_load_verity_sig_partition(
 
         /* Check if specified root hash matches if it is specified */
         if (iovec_is_set(&verity->root_hash) &&
-            iovec_memcmp(&verity->root_hash, &root_hash) != 0) {
+            !iovec_equal(&verity->root_hash, &root_hash)) {
                 _cleanup_free_ char *a = NULL, *b = NULL;
 
                 a = hexmem(root_hash.iov_base, root_hash.iov_len);
index 02f89c02ba71f2fde421906c861fac5e75e4e4cd..bddf7a74bfe11388b41128710e588414b3eb87a0 100644 (file)
@@ -7176,7 +7176,7 @@ static int tpm2_nvpcr_write_anchor_secret(
         if (r < 0) {
                 if (r != -ENOENT)
                         return log_error_errno(r, "Failed to read '%s' file: %m", joined);
-        } else if (iovec_memcmp(&existing, credential) == 0) {
+        } else if (iovec_equal(&existing, credential)) {
                 log_debug("Anchor secret file '%s' already matches expectations, not updating.", joined);
                 return 0;
         } else
@@ -8460,8 +8460,8 @@ int tpm2_pcrlock_policy_from_credentials(
                         continue;
                 }
 
-                if ((!srk || iovec_memcmp(srk, &loaded_policy.srk_handle) == 0) &&
-                    (!nv || iovec_memcmp(nv, &loaded_policy.nv_handle) == 0)) {
+                if ((!srk || iovec_equal(srk, &loaded_policy.srk_handle)) &&
+                    (!nv || iovec_equal(nv, &loaded_policy.nv_handle))) {
                         *ret = TAKE_STRUCT(loaded_policy);
                         return 1;
                 }
index b819fcd5b8586871c71bb179785ef92f882ff588..a9ef81f71da663669e2d43c6644f9fb5354ed6cd 100644 (file)
@@ -430,7 +430,7 @@ static int process_magic_file(
         /* Even if we ignore if people have non-empty files for this file, let's nonetheless warn about it,
          * so that people fix it. After all we want to retain liberty to maybe one day place some useful data
          * inside it */
-        if (iovec_memcmp(&IOVEC_MAKE(expected_hash, sizeof(expected_hash)), hash) != 0)
+        if (!iovec_equal(&IOVEC_MAKE(expected_hash, sizeof(expected_hash)), hash))
                 log_warning("Hash of best before marker file '%s' has unexpected value, proceeding anyway.", fn);
 
         usec_t best_before;
index 5cad6080933410a0d99bdbdbf018fbb320f4d122..e380550a5a70ce1b819d96020daca8b17b159717 100644 (file)
@@ -177,7 +177,7 @@ static void test_encrypt_decrypt_with(sd_id128_t mode, uid_t uid) {
                         &decrypted);
         ASSERT_OK(r);
 
-        ASSERT_EQ(iovec_memcmp(&plaintext, &decrypted), 0);
+        ASSERT_TRUE(iovec_equal(&plaintext, &decrypted));
 }
 
 static bool try_tpm2(void) {
index 575e2c52ed7df5e25ed3377685fb55c9bc0b1496..a752b1c7cda23261b081de4bdd68353996cc6a1f 100644 (file)
@@ -702,13 +702,13 @@ TEST(write_data_file_atomic_at) {
 
         _cleanup_(iovec_done) struct iovec ra = {};
         ASSERT_OK(read_full_file("/tmp/wdfa", (char**) &ra.iov_base, &ra.iov_len));
-        ASSERT_EQ(iovec_memcmp(&a, &ra), 0);
+        ASSERT_TRUE(iovec_equal(&a, &ra));
         ASSERT_OK_ERRNO(unlink("/tmp/wdfa"));
 
         ASSERT_OK(write_data_file_atomic_at(XAT_FDROOT, "tmp/wdfa", &a, /* flags= */ 0));
         iovec_done(&ra);
         ASSERT_OK(read_full_file("/tmp/wdfa", (char**) &ra.iov_base, &ra.iov_len));
-        ASSERT_EQ(iovec_memcmp(&a, &ra), 0);
+        ASSERT_TRUE(iovec_equal(&a, &ra));
         ASSERT_OK_ERRNO(unlink("/tmp/wdfa"));
 
         ASSERT_ERROR(write_data_file_atomic_at(AT_FDCWD, NULL, &a, /* flags= */ 0), EINVAL);
@@ -724,13 +724,13 @@ TEST(write_data_file_atomic_at) {
         ASSERT_OK(write_data_file_atomic_at(AT_FDCWD, "wdfa", &a, /* flags= */ 0));
         iovec_done(&ra);
         ASSERT_OK(read_full_file("/tmp/wdfa", (char**) &ra.iov_base, &ra.iov_len));
-        ASSERT_EQ(iovec_memcmp(&a, &ra), 0);
+        ASSERT_TRUE(iovec_equal(&a, &ra));
         ASSERT_OK_ERRNO(unlink("/tmp/wdfa"));
 
         ASSERT_OK(write_data_file_atomic_at(XAT_FDROOT, "tmp/wdfa", &a, /* flags= */ 0));
         iovec_done(&ra);
         ASSERT_OK(read_full_file("/tmp/wdfa", (char**) &ra.iov_base, &ra.iov_len));
-        ASSERT_EQ(iovec_memcmp(&a, &ra), 0);
+        ASSERT_TRUE(iovec_equal(&a, &ra));
         ASSERT_OK_ERRNO(unlink("/tmp/wdfa"));
 
         ASSERT_OK_ERRNO(chdir(cwd));
@@ -739,7 +739,7 @@ TEST(write_data_file_atomic_at) {
         ASSERT_OK(write_data_file_atomic_at(XAT_FDROOT, "tmp/zzz/wdfa", &a, WRITE_DATA_FILE_MKDIR_0755));
         iovec_done(&ra);
         ASSERT_OK(read_full_file("/tmp/zzz/wdfa", (char**) &ra.iov_base, &ra.iov_len));
-        ASSERT_EQ(iovec_memcmp(&a, &ra), 0);
+        ASSERT_TRUE(iovec_equal(&a, &ra));
         ASSERT_OK_ERRNO(unlink("/tmp/zzz/wdfa"));
 
         ASSERT_ERROR(write_data_file_atomic_at(AT_FDCWD, "/tmp/zzz", &a, /* flags= */ 0), EEXIST);
index 6764d2e78eb6b2046bf466b45614e2905985fbf8..791a041ab8d6fbc70d75f3e6517448c46ffe331c 100644 (file)
@@ -97,9 +97,9 @@ TEST(iovw_put_iov) {
         ASSERT_OK(iovw_put_iov(&iovw, &IOVEC_MAKE_STRING("bbb")));
         ASSERT_OK(iovw_put_iov(&iovw, &IOVEC_MAKE_STRING("ccc")));
         ASSERT_EQ(iovw.count, 3U);
-        ASSERT_EQ(iovec_memcmp(&iovw.iovec[0], &IOVEC_MAKE_STRING("aaa")), 0);
-        ASSERT_EQ(iovec_memcmp(&iovw.iovec[1], &IOVEC_MAKE_STRING("bbb")), 0);
-        ASSERT_EQ(iovec_memcmp(&iovw.iovec[2], &IOVEC_MAKE_STRING("ccc")), 0);
+        ASSERT_TRUE(iovec_equal(&iovw.iovec[0], &IOVEC_MAKE_STRING("aaa")));
+        ASSERT_TRUE(iovec_equal(&iovw.iovec[1], &IOVEC_MAKE_STRING("bbb")));
+        ASSERT_TRUE(iovec_equal(&iovw.iovec[2], &IOVEC_MAKE_STRING("ccc")));
 }
 
 TEST(iovw_put_iovw) {
@@ -123,12 +123,12 @@ TEST(iovw_put_iovw) {
 
         ASSERT_OK(iovw_put_iovw(&target, &source));
         ASSERT_EQ(target.count, 6U);
-        ASSERT_EQ(iovec_memcmp(&target.iovec[0], &IOVEC_MAKE_STRING("xxx")), 0);
-        ASSERT_EQ(iovec_memcmp(&target.iovec[1], &IOVEC_MAKE_STRING("yyy")), 0);
-        ASSERT_EQ(iovec_memcmp(&target.iovec[2], &IOVEC_MAKE_STRING("zzz")), 0);
-        ASSERT_EQ(iovec_memcmp(&target.iovec[3], &IOVEC_MAKE_STRING("aaa")), 0);
-        ASSERT_EQ(iovec_memcmp(&target.iovec[4], &IOVEC_MAKE_STRING("bbb")), 0);
-        ASSERT_EQ(iovec_memcmp(&target.iovec[5], &IOVEC_MAKE_STRING("ccc")), 0);
+        ASSERT_TRUE(iovec_equal(&target.iovec[0], &IOVEC_MAKE_STRING("xxx")));
+        ASSERT_TRUE(iovec_equal(&target.iovec[1], &IOVEC_MAKE_STRING("yyy")));
+        ASSERT_TRUE(iovec_equal(&target.iovec[2], &IOVEC_MAKE_STRING("zzz")));
+        ASSERT_TRUE(iovec_equal(&target.iovec[3], &IOVEC_MAKE_STRING("aaa")));
+        ASSERT_TRUE(iovec_equal(&target.iovec[4], &IOVEC_MAKE_STRING("bbb")));
+        ASSERT_TRUE(iovec_equal(&target.iovec[5], &IOVEC_MAKE_STRING("ccc")));
 
         /* iovw_put_iovw() does not copy data, hence the pointers must be equal */
         ASSERT_PTR_EQ(target.iovec[3].iov_base, source.iovec[0].iov_base);
@@ -137,9 +137,9 @@ TEST(iovw_put_iovw) {
 
         /* Source is unchanged */
         ASSERT_EQ(source.count, 3U);
-        ASSERT_EQ(iovec_memcmp(&source.iovec[0], &IOVEC_MAKE_STRING("aaa")), 0);
-        ASSERT_EQ(iovec_memcmp(&source.iovec[1], &IOVEC_MAKE_STRING("bbb")), 0);
-        ASSERT_EQ(iovec_memcmp(&source.iovec[2], &IOVEC_MAKE_STRING("ccc")), 0);
+        ASSERT_TRUE(iovec_equal(&source.iovec[0], &IOVEC_MAKE_STRING("aaa")));
+        ASSERT_TRUE(iovec_equal(&source.iovec[1], &IOVEC_MAKE_STRING("bbb")));
+        ASSERT_TRUE(iovec_equal(&source.iovec[2], &IOVEC_MAKE_STRING("ccc")));
 
         /* Cannot pass the same objects */
         ASSERT_ERROR(iovw_put_iovw(&target, &target), EINVAL);
@@ -183,9 +183,9 @@ TEST(iovw_extend_iov) {
         ASSERT_OK(iovw_extend_iov(&iovw, &IOVEC_MAKE_STRING("bbb")));
         ASSERT_OK(iovw_extend_iov(&iovw, &IOVEC_MAKE_STRING("ccc")));
         ASSERT_EQ(iovw.count, 3U);
-        ASSERT_EQ(iovec_memcmp(&iovw.iovec[0], &IOVEC_MAKE_STRING("aaa")), 0);
-        ASSERT_EQ(iovec_memcmp(&iovw.iovec[1], &IOVEC_MAKE_STRING("bbb")), 0);
-        ASSERT_EQ(iovec_memcmp(&iovw.iovec[2], &IOVEC_MAKE_STRING("ccc")), 0);
+        ASSERT_TRUE(iovec_equal(&iovw.iovec[0], &IOVEC_MAKE_STRING("aaa")));
+        ASSERT_TRUE(iovec_equal(&iovw.iovec[1], &IOVEC_MAKE_STRING("bbb")));
+        ASSERT_TRUE(iovec_equal(&iovw.iovec[2], &IOVEC_MAKE_STRING("ccc")));
 }
 
 TEST(iovw_extend_iovw) {
@@ -410,7 +410,7 @@ TEST(iovw_concat) {
         ASSERT_OK(iovw_put(&iovw, (char*) "bar", 4));
 
         ASSERT_OK(iovw_concat(&iovw, &iov));
-        ASSERT_EQ(iovec_memcmp(&iov, &IOVEC_MAKE("foo\0bar\0", 8)), 0);
+        ASSERT_TRUE(iovec_equal(&iov, &IOVEC_MAKE("foo\0bar\0", 8)));
 }
 
 TEST(iovw_to_cstring) {
index 1bd5d94f987d922304c137c35749e6062ca38c74..1785ef416f5b17ffb452d45ddf4cc9d8bc4083f0 100644 (file)
@@ -1206,8 +1206,8 @@ TEST(json_iovec) {
         assert_se(json_variant_unbase64_iovec(sd_json_variant_by_key(j, "nr1"), &a) >= 0);
         assert_se(json_variant_unhex_iovec(sd_json_variant_by_key(j, "nr2"), &b) >= 0);
 
-        assert_se(iovec_memcmp(&iov1, &a) == 0);
-        assert_se(iovec_memcmp(&iov2, &b) == 0);
+        assert_se(iovec_equal(&iov1, &a));
+        assert_se(iovec_equal(&iov2, &b));
         assert_se(iovec_memcmp(&iov2, &a) < 0);
         assert_se(iovec_memcmp(&iov1, &b) > 0);
 }
index a62f54006e20fccde6a0ef14983135d6cbbd046c..fb23776b211d240d99a91e92dfc79298f0277e6b 100644 (file)
@@ -104,9 +104,9 @@ static void test_log_format_iovec_sentinel(
 
         for (size_t i = 0; i < n; i++)
                 if (i < m)
-                        ASSERT_EQ(iovec_memcmp(&iovec[i], &IOVEC_MAKE_STRING(v[i])), 0);
+                        ASSERT_TRUE(iovec_equal(&iovec[i], &IOVEC_MAKE_STRING(v[i])));
                 else {
-                        ASSERT_EQ(iovec_memcmp(&iovec[i], &IOVEC_MAKE_STRING(expected[i - m])), 0);
+                        ASSERT_TRUE(iovec_equal(&iovec[i], &IOVEC_MAKE_STRING(expected[i - m])));
                         free(iovec[i].iov_base);
                 }
 
@@ -122,12 +122,12 @@ static void test_log_format_iovec_sentinel(
 
         for (size_t i = 0; i < n; i++)
                 if (i < m)
-                        ASSERT_EQ(iovec_memcmp(&iovec[i], &IOVEC_MAKE_STRING(v[i])), 0);
+                        ASSERT_TRUE(iovec_equal(&iovec[i], &IOVEC_MAKE_STRING(v[i])));
                 else if ((i - m) % 2 == 0) {
-                        ASSERT_EQ(iovec_memcmp(&iovec[i], &IOVEC_MAKE_STRING(expected[(i - m) / 2])), 0);
+                        ASSERT_TRUE(iovec_equal(&iovec[i], &IOVEC_MAKE_STRING(expected[(i - m) / 2])));
                         free(iovec[i].iov_base);
                 } else
-                        ASSERT_EQ(iovec_memcmp(&iovec[i], &IOVEC_MAKE_STRING("\n")), 0);
+                        ASSERT_TRUE(iovec_equal(&iovec[i], &IOVEC_MAKE_STRING("\n")));
 }
 
 #define test_log_format_iovec_one(...)                 \
index a6164f2677d520623fec90f342dbeba31aa1ca7e..78e2d234ab5c74bf0e8531df905442881ec3fe4d 100644 (file)
@@ -1271,7 +1271,7 @@ static void check_seal_unseal_for_handle(Tpm2Context *c, TPM2_HANDLE handle) {
                         &srk,
                         &unsealed_secret) >= 0);
 
-        assert_se(iovec_memcmp(&secret, &unsealed_secret) == 0);
+        assert_se(iovec_equal(&secret, &unsealed_secret));
 }
 
 static void check_seal_unseal(Tpm2Context *c) {