]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
refactor: replace sizeof in loop with ELEMENTSOF & FOREACH_ELEMENT (#34863)
authorIntegral <integral@member.fsf.org>
Wed, 23 Oct 2024 08:32:02 +0000 (16:32 +0800)
committerGitHub <noreply@github.com>
Wed, 23 Oct 2024 08:32:02 +0000 (10:32 +0200)
src/basic/gunicode.c
src/test/test-tpm2.c
src/udev/scsi_id/scsi_serial.c
src/udev/udev-builtin-input_id.c

index 36beb957e10014a273115dff5036db83d7b7dcb0..d7496f3e211c1b0eff52c2a5122fe68cdc49aa4b 100644 (file)
@@ -6,6 +6,7 @@
  */
 
 #include "gunicode.h"
+#include "macro.h"
 
 #define unichar uint32_t
 
@@ -92,7 +93,7 @@ unichar_iswide (unichar c)
     {0x20000, 0x2FFFD}, {0x30000, 0x3FFFD},
   };
 
-  if (bsearch ((void *)(uintptr_t)c, wide, (sizeof (wide) / sizeof ((wide)[0])), sizeof wide[0],
+  if (bsearch ((void *)(uintptr_t)c, wide, ELEMENTSOF(wide), sizeof wide[0],
                interval_compare))
     return true;
 
index de2889b8d28b8c06e9757d0ee6a02b66d1174c87..8caf51b260a662825f16f1f3d62037947754dd63 100644 (file)
@@ -74,16 +74,15 @@ TEST(tpm2_util_pbkdf2_hmac_sha256) {
         };
 
         uint8_t res[SHA256_DIGEST_SIZE];
-        for (size_t i = 0; i < sizeof(test_vectors)/sizeof(test_vectors[0]); i++) {
-
+        FOREACH_ELEMENT(vector, test_vectors) {
                 int rc = tpm2_util_pbkdf2_hmac_sha256(
-                                test_vectors[i].pass,
-                                test_vectors[i].passlen,
-                                test_vectors[i].salt,
-                                test_vectors[i].saltlen,
+                                vector->pass,
+                                vector->passlen,
+                                vector->salt,
+                                vector->saltlen,
                                 res);
                 assert_se(rc == 0);
-                assert_se(memcmp(test_vectors[i].expected, res, SHA256_DIGEST_SIZE) == 0);
+                assert_se(memcmp(vector->expected, res, SHA256_DIGEST_SIZE) == 0);
         }
 }
 
index aed6082620e33139a64a76cf14030a23ee42d35c..d8655c08c3b672223d6c37f9046b96a3e57b4a88 100644 (file)
@@ -557,7 +557,6 @@ static int do_scsi_page83_inquiry(struct scsi_id_device *dev_scsi, int fd,
                                   char *unit_serial_number, char *wwn,
                                   char *wwn_vendor_extension, char *tgpt_group) {
         int retval;
-        unsigned id_ind, j;
         unsigned char page_83[SCSI_INQ_BUFF_LEN];
 
         /* also pick up the page 80 serial number */
@@ -611,16 +610,14 @@ static int do_scsi_page83_inquiry(struct scsi_id_device *dev_scsi, int fd,
          * Search for a match in the prioritized id_search_list - since WWN ids
          * come first we can pick up the WWN in check_fill_0x83_id().
          */
-        for (id_ind = 0;
-             id_ind < sizeof(id_search_list)/sizeof(id_search_list[0]);
-             id_ind++) {
+        FOREACH_ELEMENT(search_value, id_search_list) {
                 /*
                  * Examine each descriptor returned. There is normally only
                  * one or a small number of descriptors.
                  */
-                for (j = 4; j <= ((unsigned)page_83[2] << 8) + (unsigned)page_83[3] + 3; j += page_83[j + 3] + 4) {
+                for (unsigned j = 4; j <= ((unsigned)page_83[2] << 8) + (unsigned)page_83[3] + 3; j += page_83[j + 3] + 4) {
                         retval = check_fill_0x83_id(dev_scsi, page_83 + j,
-                                                    id_search_list + id_ind,
+                                                    search_value,
                                                     serial, serial_short, len,
                                                     wwn, wwn_vendor_extension,
                                                     tgpt_group);
index 6f75d9df22c9a32dd8631f3d531b977ec03d4083..1dec5f868ef4484fce59df5d7c0dae7a17a95006 100644 (file)
@@ -356,7 +356,7 @@ static bool test_key(
                                  i * BITS_PER_LONG, yes_no(found));
         }
         /* If there are no keys in the lower block, check the higher blocks */
-        for (size_t block = 0; block < sizeof(high_key_blocks) / sizeof(struct range) && !found; block++)
+        for (size_t block = 0; block < ELEMENTSOF(high_key_blocks) && !found; block++)
                 for (unsigned i = high_key_blocks[block].start; i < high_key_blocks[block].end && !found; i++)
                         if (test_bit(i, bitmask_key)) {
                                 log_device_debug(dev, "test_key: Found key %x in high block", i);