diff options
author | Lennart Poettering <lennart@poettering.net> | 2021-09-13 12:33:21 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2021-09-13 12:48:27 +0200 |
commit | 3f9992d82e0bc601bb33ad7b19a1722c4478c542 (patch) | |
tree | 478b35960ec78cd91ba6f9a23051a40b4a3ebeda /src/basic | |
parent | 503994bada629dbce23ef58fb71f67296b7d2b45 (diff) | |
download | systemd-3f9992d82e0bc601bb33ad7b19a1722c4478c542.tar.gz |
memory-util: replace memeqzero() by a more generic memeqbyte()
The new helper can check for any byte, no just zeroes. The old name is
then converted into a macro that wraps our new version of the helper.
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/memory-util.c | 13 | ||||
-rw-r--r-- | src/basic/memory-util.h | 4 |
2 files changed, 9 insertions, 8 deletions
diff --git a/src/basic/memory-util.c b/src/basic/memory-util.c index 3338e355f7..2983762117 100644 --- a/src/basic/memory-util.c +++ b/src/basic/memory-util.c @@ -18,26 +18,25 @@ size_t page_size(void) { return pgsz; } -bool memeqzero(const void *data, size_t length) { - /* Does the buffer consist entirely of NULs? +bool memeqbyte(uint8_t byte, const void *data, size_t length) { + /* Does the buffer consist entirely of the same specific byte value? * Copied from https://github.com/systemd/casync/, copied in turn from * https://github.com/rustyrussell/ccan/blob/master/ccan/mem/mem.c#L92, * which is licensed CC-0. */ const uint8_t *p = data; - size_t i; /* Check first 16 bytes manually */ - for (i = 0; i < 16; i++, length--) { + for (size_t i = 0; i < 16; i++, length--) { if (length == 0) return true; - if (p[i]) + if (p[i] != byte) return false; } - /* Now we know first 16 bytes are NUL, memcmp with self. */ - return memcmp(data, p + i, length) == 0; + /* Now we know first 16 bytes match, memcmp() with self. */ + return memcmp(data, p + 16, length) == 0; } #if !HAVE_EXPLICIT_BZERO diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index e2b543cb8f..9f37431fc1 100644 --- a/src/basic/memory-util.h +++ b/src/basic/memory-util.h @@ -47,7 +47,9 @@ static inline int memcmp_nn(const void *s1, size_t n1, const void *s2, size_t n2 #define zero(x) (memzero(&(x), sizeof(x))) -bool memeqzero(const void *data, size_t length); +bool memeqbyte(uint8_t byte, const void *data, size_t length); + +#define memeqzero(data, length) memeqbyte(0x00, data, length) #define eqzero(x) memeqzero(x, sizeof(x)) |