From 8208c8f25d47d9631a43969e3d458a94ad07b1d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 8 Dec 2017 17:28:02 +0100 Subject: [PATCH] shared: use _cleanup_ in specifier_printf (#7586) Follow-up for e62d316561. --- src/shared/specifier.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/shared/specifier.c b/src/shared/specifier.c index 69fefc8d008..23aaa88c4bd 100644 --- a/src/shared/specifier.c +++ b/src/shared/specifier.c @@ -46,10 +46,11 @@ #define POSSIBLE_SPECIFIERS ALPHANUMERICAL "%" int specifier_printf(const char *text, const Specifier table[], void *userdata, char **_ret) { - char *ret, *t; + size_t l; + _cleanup_free_ char *ret = NULL; + char *t; const char *f; bool percent = false; - size_t l; int r; assert(text); @@ -80,32 +81,25 @@ int specifier_printf(const char *text, const Specifier table[], void *userdata, size_t k, j; r = i->lookup(i->specifier, i->data, userdata, &w); - if (r < 0) { - free(ret); + if (r < 0) return r; - } j = t - ret; k = strlen(w); n = new(char, j + k + l + 1); - if (!n) { - free(ret); + if (!n) return -ENOMEM; - } memcpy(n, ret, j); memcpy(n + j, w, k); - free(ret); - - ret = n; - t = n + j + k; - } else if (strchr(POSSIBLE_SPECIFIERS, *f)) { + free_and_replace(ret, n); + t = ret + j + k; + } else if (strchr(POSSIBLE_SPECIFIERS, *f)) /* Oops, an unknown specifier. */ - free(ret); return -EBADSLT; - } else { + else { *(t++) = '%'; *(t++) = *f; } @@ -124,6 +118,7 @@ int specifier_printf(const char *text, const Specifier table[], void *userdata, *t = 0; *_ret = ret; + ret = NULL; return 0; }