File 2004-libefi-util-introduce-xrealloc_extra_-k-_item-s.patch of Package systemd

From e4ba48d7896ffeea5d902a848dca361eb75dbe50 Mon Sep 17 00:00:00 2001
From: Raito Bezarius <masterancpp@gmail.com>
Date: Mon, 19 Jun 2023 14:41:38 +0200
Subject: [PATCH 2004/2007] libefi(util): introduce
 `xrealloc_extra_(k)_item(s)`

This is helpful to extend of K items an existing NULL-terminated array,
by default, we use it with an extension of K=1, in `boot.c` for loading a list of initrds
and addons.
---
 src/boot/boot.c | 14 ++++++++------
 src/boot/util.h |  9 +++++++++
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/src/boot/boot.c b/src/boot/boot.c
index 509e0d9b84..7fe88f41d2 100644
--- a/src/boot/boot.c
+++ b/src/boot/boot.c
@@ -1507,17 +1507,19 @@ static void boot_entry_add_type1(
                         entry->devicetree = xstr8_to_path(value);
 
                 } else if (streq8(key, "initrd")) {
-                        entry->initrd = xrealloc(
+                        entry->initrd = xrealloc_extra_item(
+                                uint16_t,
                                 entry->initrd,
-                                n_initrd == 0 ? 0 : (n_initrd + 1) * sizeof(uint16_t *),
-                                (n_initrd + 2) * sizeof(uint16_t *));
+                                n_initrd
+                        );
                         entry->initrd[n_initrd++] = xstr8_to_path(value);
                         entry->initrd[n_initrd] = NULL;
                 } else if (streq8(key, "add-on")) {
-                        entry->addons = xrealloc(
+                        entry->addons = xrealloc_extra_item(
+                                uint16_t,
                                 entry->addons,
-                                n_addons == 0 ? 0 : (n_addons + 1) * sizeof(uint16_t *),
-                                (n_addons + 2) * sizeof(uint16_t *));
+                                n_addons
+                        );
                         entry->addons[n_addons++] = xstr8_to_path(value);
                         entry->addons[n_addons] = NULL;
                 } else if (streq8(key, "options")) {
diff --git a/src/boot/util.h b/src/boot/util.h
index af14a0e31e..550af4e434 100644
--- a/src/boot/util.h
+++ b/src/boot/util.h
@@ -72,6 +72,15 @@ static inline void* xmemdup(const void *p, size_t l) {
 #define xnew0(type, n) ((type *) xcalloc_multiply((n), sizeof(type)))
 
 bool free_and_xstrdup16(char16_t **p, const char16_t *s);
+static inline void **xrealloc_extra_k_items(void *p, size_t item_size, size_t current_size, size_t extra_elements) {
+        size_t new_size = (current_size + extra_elements + 1) * item_size;
+        if (current_size == 0)
+                return (void**)xrealloc(p, 0, new_size);
+        else
+                return (void**)xrealloc(p, (current_size + 1) * item_size, new_size);
+}
+
+#define xrealloc_extra_item(type, p, current_size) ((type **) xrealloc_extra_k_items(p, sizeof(type *), current_size, 1))
 
 typedef struct {
         EFI_PHYSICAL_ADDRESS addr;
-- 
2.49.0

openSUSE Build Service is sponsored by