From: Paul Eggert Date: Wed, 17 Sep 2025 19:12:53 +0000 (-0700) Subject: maint: prefer memeq to memcmp X-Git-Tag: v9.8~30 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=52576367611ee1101fecec5b8892a90da618f32b;p=thirdparty%2Fcoreutils.git maint: prefer memeq to memcmp * gl/modules/randread-tests (Depends-on): Add stringeq. * gl/tests/test-rand-isaac.c (main): * src/copy.c (source_is_dst_backup): * src/digest.c (b64_equal): * src/install.c (have_same_content): * src/ls.c (main, parse_ls_color): * src/nl.c (check_section): * src/od.c (write_block): * src/seq.c (seq_fast): * src/stty.c (eq_mode): * src/system.h (is_nul): Prefer memeq to memcmp when either will do. --- diff --git a/gl/modules/randread-tests b/gl/modules/randread-tests index af24c90414..35da6cdec0 100644 --- a/gl/modules/randread-tests +++ b/gl/modules/randread-tests @@ -3,6 +3,7 @@ tests/test-rand-isaac.c tests/macros.h Depends-on: +stringeq configure.ac: diff --git a/gl/tests/test-rand-isaac.c b/gl/tests/test-rand-isaac.c index 334a7601e6..5457d0f3c2 100644 --- a/gl/tests/test-rand-isaac.c +++ b/gl/tests/test-rand-isaac.c @@ -576,7 +576,7 @@ main (int argc, char **argv) for (i = 0; i < sizeof expected / sizeof expected[0]; i++) { isaac_refill (&s, r); - ASSERT (memcmp (r, expected[i], sizeof r) == 0); + ASSERT (memeq (r, expected[i], sizeof r)); } /* If invoked with a positive argument, run a benchmark; diff --git a/src/copy.c b/src/copy.c index 11c82e1a81..4af66c8821 100644 --- a/src/copy.c +++ b/src/copy.c @@ -1597,7 +1597,7 @@ source_is_dst_backup (char const *srcbase, struct stat const *src_st, size_t dstbaselen = strlen (dstbase); size_t suffixlen = strlen (simple_backup_suffix); if (! (srcbaselen == dstbaselen + suffixlen - && memcmp (srcbase, dstbase, dstbaselen) == 0 + && memeq (srcbase, dstbase, dstbaselen) && streq (srcbase + dstbaselen, simple_backup_suffix))) return false; char *dst_back = subst_suffix (dst_relname, diff --git a/src/digest.c b/src/digest.c index 03b3190d6b..64c2c49b1d 100644 --- a/src/digest.c +++ b/src/digest.c @@ -1191,7 +1191,7 @@ b64_equal (unsigned char const *b64_digest, unsigned char const *bin_buffer) size_t b64_n_bytes = BASE64_LENGTH (digest_length / 8); char b64[BASE64_LENGTH (DIGEST_BIN_BYTES) + 1]; base64_encode ((char const *) bin_buffer, digest_length / 8, b64, sizeof b64); - return memcmp (b64_digest, b64, b64_n_bytes + 1) == 0; + return memeq (b64_digest, b64, b64_n_bytes + 1); } #endif diff --git a/src/install.c b/src/install.c index 53f6a2ff57..f8a8140254 100644 --- a/src/install.c +++ b/src/install.c @@ -147,7 +147,7 @@ have_same_content (int a_fd, int b_fd) if (size != full_read (b_fd, b_buff, sizeof b_buff)) return false; - if (memcmp (a_buff, b_buff, size) != 0) + if (!memeq (a_buff, b_buff, size)) return false; } diff --git a/src/ls.c b/src/ls.c index 323b1db10e..d999d5bdf1 100644 --- a/src/ls.c +++ b/src/ls.c @@ -1870,7 +1870,7 @@ main (int argc, char **argv) /* Skip the restore when it would be a no-op, i.e., when left is "\033[" and right is "m". */ if (!(color_indicator[C_LEFT].len == 2 - && memcmp (color_indicator[C_LEFT].string, "\033[", 2) == 0 + && memeq (color_indicator[C_LEFT].string, "\033[", 2) && color_indicator[C_RIGHT].len == 1 && color_indicator[C_RIGHT].string[0] == 'm')) restore_default_color (); @@ -2894,7 +2894,7 @@ parse_ls_color (void) { if (e2->ext.len < SIZE_MAX && e1->ext.len == e2->ext.len) { - if (memcmp (e1->ext.string, e2->ext.string, e1->ext.len) == 0) + if (memeq (e1->ext.string, e2->ext.string, e1->ext.len)) e2->ext.len = SIZE_MAX; /* Ignore */ else if (c_strncasecmp (e1->ext.string, e2->ext.string, e1->ext.len) == 0) @@ -2904,8 +2904,8 @@ parse_ls_color (void) e2->ext.len = SIZE_MAX; /* Ignore */ } else if (e1->seq.len == e2->seq.len - && memcmp (e1->seq.string, e2->seq.string, - e1->seq.len) == 0) + && memeq (e1->seq.string, e2->seq.string, + e1->seq.len)) { e2->ext.len = SIZE_MAX; /* Ignore */ case_ignored = true; /* Ignore all subsequent */ diff --git a/src/nl.c b/src/nl.c index 2812b383b7..f243a29536 100644 --- a/src/nl.c +++ b/src/nl.c @@ -389,16 +389,16 @@ check_section (void) size_t len = line_buf.length - 1; if (len < 2 || footer_del_len < 2 - || memcmp (line_buf.buffer, section_del, 2)) + || !memeq (line_buf.buffer, section_del, 2)) return Text; if (len == header_del_len - && !memcmp (line_buf.buffer, header_del, header_del_len)) + && memeq (line_buf.buffer, header_del, header_del_len)) return Header; if (len == body_del_len - && !memcmp (line_buf.buffer, body_del, body_del_len)) + && memeq (line_buf.buffer, body_del, body_del_len)) return Body; if (len == footer_del_len - && !memcmp (line_buf.buffer, footer_del, footer_del_len)) + && memeq (line_buf.buffer, footer_del, footer_del_len)) return Footer; return Text; } diff --git a/src/od.c b/src/od.c index 45d9f32e6f..f8937298fb 100644 --- a/src/od.c +++ b/src/od.c @@ -1273,11 +1273,9 @@ write_block (intmax_t current_offset, idx_t n_bytes, static bool first = true; static bool prev_pair_equal = false; -#define EQUAL_BLOCKS(b1, b2) (memcmp (b1, b2, bytes_per_block) == 0) - if (abbreviate_duplicate_blocks && !first && n_bytes == bytes_per_block - && EQUAL_BLOCKS (prev_block, curr_block)) + && memeq (prev_block, curr_block, bytes_per_block)) { if (prev_pair_equal) { diff --git a/src/seq.c b/src/seq.c index 2bff0d40cc..c3b9a4830e 100644 --- a/src/seq.c +++ b/src/seq.c @@ -465,7 +465,7 @@ seq_fast (char const *a, char const *b, uintmax_t step) idx_t p_len = strlen (a); idx_t b_len = strlen (b); - bool inf = b_len == 3 && memcmp (b, "inf", 4) == 0; + bool inf = b_len == 3 && memeq (b, "inf", 4); /* Allow for at least 31 digits without realloc. 1 more than p_len is needed for the inf case. */ diff --git a/src/stty.c b/src/stty.c index 8032304bb4..c9523efdcd 100644 --- a/src/stty.c +++ b/src/stty.c @@ -1286,7 +1286,7 @@ apply_settings (bool checking, char const *device_name, int main (int argc, char **argv) { - /* Initialize to all zeroes so there is no risk memcmp will report a + /* Initialize to all zeroes so there is no risk memeq will report a spurious difference in an uninitialized portion of the structure. */ static struct termios mode; @@ -1424,7 +1424,7 @@ main (int argc, char **argv) if (require_set_attr) { - /* Initialize to all zeroes so there is no risk memcmp will report a + /* Initialize to all zeroes so there is no risk memeq will report a spurious difference in an uninitialized portion of the structure. */ static struct termios new_mode; @@ -1476,7 +1476,7 @@ eq_mode (struct termios *mode1, struct termios *mode2) #ifdef HAVE_C_LINE && mode1->c_line == mode2->c_line #endif - && memcmp (mode1->c_cc, mode2->c_cc, sizeof (mode1->c_cc)) == 0 + && memeq (mode1->c_cc, mode2->c_cc, sizeof (mode1->c_cc)) && cfgetispeed (mode1) == cfgetispeed (mode2) && cfgetospeed (mode1) == cfgetospeed (mode2); } diff --git a/src/system.h b/src/system.h index f37d4b811a..a7ae5a5f3b 100644 --- a/src/system.h +++ b/src/system.h @@ -496,7 +496,7 @@ is_nul (void const *buf, size_t length) #endif if (! length) - return true; + return true; /* Check len bytes not aligned on a word. */ while (UNLIKELY (length & (sizeof word - 1))) @@ -523,8 +523,8 @@ is_nul (void const *buf, size_t length) break; } - /* Now we know first 16 bytes are NUL, memcmp with self. */ - return memcmp (buf, p, length) == 0; + /* Now we know first 16 bytes are NUL, memeq with self. */ + return memeq (buf, p, length); } /* Set Accum = 10*Accum + Digit_val and return true, where Accum is an