From a4204d3335cf0fed9a9355a857759e2e20757cb3 Mon Sep 17 00:00:00 2001 From: Consolatis <35009135+Consolatis@users.noreply.github.com> Date: Fri, 15 Nov 2024 22:59:57 +0100 Subject: [PATCH] common/mem.h: introduce xstrdup_replace() --- include/common/mem.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/common/mem.h b/include/common/mem.h index 4fd5b349..d0d5fdc5 100644 --- a/include/common/mem.h +++ b/include/common/mem.h @@ -44,6 +44,14 @@ void *xrealloc(void *ptr, size_t size); */ char *xstrdup(const char *str); +/* + * Same as ptr = xstrdup(str) but free + * before assigning the result. + */ +#define xstrdup_replace(ptr, str) do { \ + free(ptr); (ptr) = xstrdup(str); \ +} while (0) + /* * Frees memory pointed to by and sets to NULL. * Does nothing if is already NULL.