From: Integral Date: Wed, 23 Oct 2024 08:32:02 +0000 (+0800) Subject: refactor: replace sizeof in loop with ELEMENTSOF & FOREACH_ELEMENT (#34863) X-Git-Tag: v257-rc1~156 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b6b8527cd184085008585c1a1f9725eb97c342ef;p=thirdparty%2Fsystemd.git refactor: replace sizeof in loop with ELEMENTSOF & FOREACH_ELEMENT (#34863) --- diff --git a/src/basic/gunicode.c b/src/basic/gunicode.c index 36beb957e10..d7496f3e211 100644 --- a/src/basic/gunicode.c +++ b/src/basic/gunicode.c @@ -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; diff --git a/src/test/test-tpm2.c b/src/test/test-tpm2.c index de2889b8d28..8caf51b260a 100644 --- a/src/test/test-tpm2.c +++ b/src/test/test-tpm2.c @@ -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); } } diff --git a/src/udev/scsi_id/scsi_serial.c b/src/udev/scsi_id/scsi_serial.c index aed6082620e..d8655c08c3b 100644 --- a/src/udev/scsi_id/scsi_serial.c +++ b/src/udev/scsi_id/scsi_serial.c @@ -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); diff --git a/src/udev/udev-builtin-input_id.c b/src/udev/udev-builtin-input_id.c index 6f75d9df22c..1dec5f868ef 100644 --- a/src/udev/udev-builtin-input_id.c +++ b/src/udev/udev-builtin-input_id.c @@ -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);