diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index d26a0918e1..c350dc1e54 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 677fe64015..f9aabb42be 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" @@ -24,8 +25,7 @@ static EFI_STATUS tpm1_measure_to_pcr_and_event_log( assert(description); 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); + tcg_event = xmalloc0(offsetof(TCG_PCR_EVENT, Event) + desc_len); *tcg_event = (TCG_PCR_EVENT) { .EventSize = desc_len, .PCRIndex = pcrindex, @@ -62,8 +62,7 @@ static EFI_STATUS tpm2_measure_to_pcr_and_event_log( * here. */ 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); + tcg_event = xmalloc0(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 493e7ed4c2..3e13303163 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 685715f36e..60991d4718 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/boot/efi/util.h b/src/boot/efi/util.h index de0c83ddb9..10620dabca 100644 --- a/src/boot/efi/util.h +++ b/src/boot/efi/util.h @@ -35,6 +35,12 @@ static inline void *xmalloc(size_t size) { return p; } +#define xmalloc0(size) \ + ({ \ + size_t _size_ = (size); \ + memzero(xmalloc(_size_), _size_); \ + }) + _malloc_ _alloc_(1, 2) _returns_nonnull_ _warn_unused_result_ static inline void *xmalloc_multiply(size_t size, size_t n) { assert_se(!__builtin_mul_overflow(size, n, &size)); diff --git a/src/fundamental/memory-util-fundamental.h b/src/fundamental/memory-util-fundamental.h index 78e2dbec59..1ffacff21a 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)