Blob Blame History Raw
From 77c10205bb337585c320e91af4b416f2dcc6faba Mon Sep 17 00:00:00 2001
From: Tom Gundersen <teg@jklm.no>
Date: Thu, 18 Sep 2014 13:47:00 +0200
Subject: [PATCH] shared: conf-parser - don't leak memory on error in
 DEFINE_CONFIG_PARSE_ENUMV

Found by Coverity. Fixes CID #1237746.
---
 src/shared/conf-parser.h | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/shared/conf-parser.h b/src/shared/conf-parser.h
index 94185152cd..62f2a01e5e 100644
--- a/src/shared/conf-parser.h
+++ b/src/shared/conf-parser.h
@@ -169,7 +169,8 @@ int log_syntax_internal(const char *unit, int level,
                      void *data,                                               \
                      void *userdata) {                                         \
                                                                                \
-                type **enums = data, *xs, x, *ys;                              \
+                type **enums = data, x, *ys;                                   \
+                _cleanup_free_ type *xs = NULL;                                \
                 const char *word, *state;                                      \
                 size_t l, i = 0;                                               \
                                                                                \
@@ -186,6 +187,7 @@ int log_syntax_internal(const char *unit, int level,
                                                                                \
                 FOREACH_WORD(word, l, rvalue, state) {                         \
                         _cleanup_free_ char *en = NULL;                        \
+                        type *new_xs;                                          \
                                                                                \
                         en = strndup(word, l);                                 \
                         if (!en)                                               \
@@ -211,8 +213,10 @@ int log_syntax_internal(const char *unit, int level,
                                 continue;                                      \
                                                                                \
                         *(xs + i) = x;                                         \
-                        xs = realloc(xs, (++i + 1) * sizeof(type));            \
-                        if (!xs)                                               \
+                        new_xs = realloc(xs, (++i + 1) * sizeof(type));        \
+                        if (new_xs)                                            \
+                                xs = new_xs;                                   \
+                        else                                                   \
                                 return -ENOMEM;                                \
                                                                                \
                         *(xs + i) = invalid;                                   \
@@ -220,5 +224,7 @@ int log_syntax_internal(const char *unit, int level,
                                                                                \
                 free(*enums);                                                  \
                 *enums = xs;                                                   \
+                xs = NULL;                                                     \
+                                                                               \
                 return 0;                                                      \
         }