From: Lennart Poettering Date: Mon, 9 Sep 2024 13:46:52 +0000 (+0200) Subject: efi: add free_and_xstrdup16() helper modelled after free_and_strdup() in userspace X-Git-Tag: v257-rc1~501^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=52dd7c813178f0472655cfeb688cae0fe76d254a;p=thirdparty%2Fsystemd.git efi: add free_and_xstrdup16() helper modelled after free_and_strdup() in userspace --- diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c index ae605bf5a50..f5f748bc6cf 100644 --- a/src/boot/efi/util.c +++ b/src/boot/efi/util.c @@ -492,3 +492,24 @@ void *xmalloc(size_t size) { assert_se(BS->AllocatePool(EfiLoaderData, size, &p) == EFI_SUCCESS); return p; } + +bool free_and_xstrdup16(char16_t **p, const char16_t *s) { + char16_t *t; + + assert(p); + + /* Replaces a string pointer with a strdup()ed new string, + * possibly freeing the old one. */ + + if (streq_ptr(*p, s)) + return false; + + if (s) + t = xstrdup16(s); + else + t = NULL; + + free(*p); + *p = t; + return true; +} diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h index d8d695ea795..50025a34a83 100644 --- a/src/boot/efi/util.h +++ b/src/boot/efi/util.h @@ -55,6 +55,8 @@ static inline void* xmemdup(const void *p, size_t l) { #define xnew(type, n) ((type *) xmalloc_multiply((n), sizeof(type))) +bool free_and_xstrdup16(char16_t **p, const char16_t *s); + typedef struct { EFI_PHYSICAL_ADDRESS addr; size_t n_pages;