From 32a81e197bce3b16113c7603a766773c1a5cdcaf Mon Sep 17 00:00:00 2001 From: nicm Date: Thu, 25 Apr 2019 18:18:55 +0000 Subject: Make options_tostring allocate its result instead of using a stack buffer (needed for something in the future). --- format.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'format.c') diff --git a/format.c b/format.c index e544e6b3..abcdb1d7 100644 --- a/format.c +++ b/format.c @@ -904,9 +904,8 @@ format_find(struct format_tree *ft, const char *key, int modifiers) struct environ_entry *envent; static char s[64]; struct options_entry *o; - const char *found; int idx; - char *copy, *saved; + char *found, *saved; if (~modifiers & FORMAT_TIMESTRING) { o = options_parse_get(global_options, key, &idx, 0); @@ -933,12 +932,11 @@ format_find(struct format_tree *ft, const char *key, int modifiers) return (NULL); ctime_r(&fe->t, s); s[strcspn(s, "\n")] = '\0'; - found = s; + found = xstrdup(s); goto found; } if (fe->t != 0) { - xsnprintf(s, sizeof s, "%lld", (long long)fe->t); - found = s; + xasprintf(&found, "%lld", (long long)fe->t); goto found; } if (fe->value == NULL && fe->cb != NULL) { @@ -946,7 +944,7 @@ format_find(struct format_tree *ft, const char *key, int modifiers) if (fe->value == NULL) fe->value = xstrdup(""); } - found = fe->value; + found = xstrdup(fe->value); goto found; } @@ -957,7 +955,7 @@ format_find(struct format_tree *ft, const char *key, int modifiers) if (envent == NULL) envent = environ_find(global_environ, key); if (envent != NULL) { - found = envent->value; + found = xstrdup(envent->value); goto found; } } @@ -967,23 +965,22 @@ format_find(struct format_tree *ft, const char *key, int modifiers) found: if (found == NULL) return (NULL); - copy = xstrdup(found); if (modifiers & FORMAT_BASENAME) { - saved = copy; - copy = xstrdup(basename(saved)); + saved = found; + found = xstrdup(basename(saved)); free(saved); } if (modifiers & FORMAT_DIRNAME) { - saved = copy; - copy = xstrdup(dirname(saved)); + saved = found; + found = xstrdup(dirname(saved)); free(saved); } if (modifiers & FORMAT_QUOTE) { - saved = copy; - copy = xstrdup(format_quote(saved)); + saved = found; + found = xstrdup(format_quote(saved)); free(saved); } - return (copy); + return (found); } /* Skip until end. */ -- cgit