util: add xmalloc0() helper

This commit is contained in:
Lennart Poettering
2023-10-02 15:02:26 +02:00
parent 4ac79c2b77
commit 5a71b83002
2 changed files with 8 additions and 4 deletions
+2 -4
View File
@@ -25,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);
memzero(tcg_event, 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,
@@ -63,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);
memzero(tcg_event, 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),
+6
View File
@@ -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));