From: Lennart Poettering Date: Mon, 2 Oct 2023 11:06:27 +0000 (+0200) Subject: memory-util: move memzero() to src/fundamental/ to share with UEFI X-Git-Tag: v255-rc1~362^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4ac79c2b77adb9ba8106bdda75f78d13979a90c9;p=thirdparty%2Fsystemd.git memory-util: move memzero() to src/fundamental/ to share with UEFI (and while we are at it, make sure it returns the input pointer as output) --- diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index d26a0918e1a..c350dc1e54b 100644 --- a/src/basic/memory-util.h +++ b/src/basic/memory-util.h @@ -47,13 +47,6 @@ static inline int memcmp_nn(const void *s1, size_t n1, const void *s2, size_t n2 ?: CMP(n1, n2); } -#define memzero(x,l) \ - ({ \ - size_t _l_ = (l); \ - if (_l_ > 0) \ - memset(x, 0, _l_); \ - }) - #define zero(x) (memzero(&(x), sizeof(x))) bool memeqbyte(uint8_t byte, const void *data, size_t length); diff --git a/src/boot/efi/measure.c b/src/boot/efi/measure.c index 677fe640153..6f7282dc7db 100644 --- a/src/boot/efi/measure.c +++ b/src/boot/efi/measure.c @@ -4,6 +4,7 @@ #include "macro-fundamental.h" #include "measure.h" +#include "memory-util-fundamental.h" #include "proto/tcg.h" #include "tpm2-pcr.h" #include "util.h" @@ -25,7 +26,7 @@ static EFI_STATUS tpm1_measure_to_pcr_and_event_log( desc_len = strsize16(description); tcg_event = xmalloc(offsetof(TCG_PCR_EVENT, Event) + desc_len); - memset(tcg_event, 0, offsetof(TCG_PCR_EVENT, Event) + desc_len); + memzero(tcg_event, offsetof(TCG_PCR_EVENT, Event) + desc_len); *tcg_event = (TCG_PCR_EVENT) { .EventSize = desc_len, .PCRIndex = pcrindex, @@ -63,7 +64,7 @@ static EFI_STATUS tpm2_measure_to_pcr_and_event_log( desc_len = strsize16(description); tcg_event = xmalloc(offsetof(EFI_TCG2_EVENT, Event) + desc_len); - memset(tcg_event, 0, offsetof(EFI_TCG2_EVENT, Event) + desc_len); + memzero(tcg_event, offsetof(EFI_TCG2_EVENT, Event) + desc_len); *tcg_event = (EFI_TCG2_EVENT) { .Size = offsetof(EFI_TCG2_EVENT, Event) + desc_len, .Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER), diff --git a/src/boot/efi/stub.c b/src/boot/efi/stub.c index 493e7ed4c2e..3e133031630 100644 --- a/src/boot/efi/stub.c +++ b/src/boot/efi/stub.c @@ -6,6 +6,7 @@ #include "graphics.h" #include "linux.h" #include "measure.h" +#include "memory-util-fundamental.h" #include "part-discovery.h" #include "pe.h" #include "proto/shell-parameters.h" @@ -64,7 +65,7 @@ static EFI_STATUS combine_initrd( pad = ALIGN4(initrd_size) - initrd_size; if (pad > 0) { - memset(p, 0, pad); + memzero(p, pad); p += pad; } } diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c index 685715f36e7..60991d47182 100644 --- a/src/boot/efi/util.c +++ b/src/boot/efi/util.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "device-path-util.h" +#include "memory-util-fundamental.h" #include "proto/device-path.h" #include "proto/simple-text-io.h" #include "ticks.h" @@ -373,7 +374,7 @@ EFI_STATUS file_read(EFI_FILE *dir, const char16_t *name, size_t off, size_t siz return err; /* Note that chunked_read() changes size to reflect the actual bytes read. */ - memset(buf + size, 0, extra); + memzero(buf + size, extra); *ret = TAKE_PTR(buf); if (ret_size) diff --git a/src/fundamental/memory-util-fundamental.h b/src/fundamental/memory-util-fundamental.h index 78e2dbec598..1ffacff21a9 100644 --- a/src/fundamental/memory-util-fundamental.h +++ b/src/fundamental/memory-util-fundamental.h @@ -11,6 +11,12 @@ #include "macro-fundamental.h" +#define memzero(x, l) \ + ({ \ + size_t _l_ = (l); \ + _l_ > 0 ? memset((x), 0, _l_) : (x); \ + }) + #if !SD_BOOT && HAVE_EXPLICIT_BZERO static inline void *explicit_bzero_safe(void *p, size_t l) { if (p && l > 0)