a4b143
From 64d6d09b47d22db8a82373229c828e7cf1373955 Mon Sep 17 00:00:00 2001
a4b143
From: Lennart Poettering <lennart@poettering.net>
a4b143
Date: Tue, 1 Oct 2013 00:08:30 +0200
a4b143
Subject: [PATCH] local: fix memory leak when putting together locale settings
a4b143
a4b143
Also, we need to use proper strv_env_xyz() calls when putting together
a4b143
the environment array, since otherwise settings won't be properly
a4b143
overriden.
a4b143
a4b143
And let's get rid of strv_appendf(), is overkill and there was only one
a4b143
user.
a4b143
---
a4b143
 src/core/locale-setup.c | 31 +++++++++++++++++++++++++++----
a4b143
 src/core/manager.c      |  4 +++-
a4b143
 src/shared/strv.c       | 15 ---------------
a4b143
 src/shared/strv.h       |  1 -
a4b143
 4 files changed, 30 insertions(+), 21 deletions(-)
a4b143
a4b143
diff --git a/src/core/locale-setup.c b/src/core/locale-setup.c
a4b143
index 31374ac..276deb9 100644
a4b143
--- a/src/core/locale-setup.c
a4b143
+++ b/src/core/locale-setup.c
a4b143
@@ -29,6 +29,7 @@
a4b143
 #include "virt.h"
a4b143
 #include "fileio.h"
a4b143
 #include "strv.h"
a4b143
+#include "env-util.h"
a4b143
 
a4b143
 enum {
a4b143
         /* We don't list LC_ALL here on purpose. People should be
a4b143
@@ -69,7 +70,7 @@ static const char * const variable_names[_VARIABLE_MAX] = {
a4b143
 };
a4b143
 
a4b143
 int locale_setup(char ***environment) {
a4b143
-        char **env;
a4b143
+        char **add;
a4b143
         char *variables[_VARIABLE_MAX] = {};
a4b143
         int r = 0, i;
a4b143
 
a4b143
@@ -119,22 +120,44 @@ int locale_setup(char ***environment) {
a4b143
                         log_warning("Failed to read /etc/locale.conf: %s", strerror(-r));
a4b143
         }
a4b143
 
a4b143
+        add = NULL;
a4b143
         for (i = 0; i < _VARIABLE_MAX; i++) {
a4b143
+                char *s;
a4b143
+
a4b143
                 if (!variables[i])
a4b143
                         continue;
a4b143
 
a4b143
-                env = strv_appendf(*environment, "%s=%s", variable_names[i], variables[i]);
a4b143
-                if (!env) {
a4b143
+                s = strjoin(variable_names[i], "=", variables[i], NULL);
a4b143
+                if (!s) {
a4b143
+                        r = -ENOMEM;
a4b143
+                        goto finish;
a4b143
+                }
a4b143
+
a4b143
+                if (strv_push(&add, s) < 0) {
a4b143
+                        free(s);
a4b143
+                        r = -ENOMEM;
a4b143
+                        goto finish;
a4b143
+                }
a4b143
+        }
a4b143
+
a4b143
+        if (!strv_isempty(add)) {
a4b143
+                char **e;
a4b143
+
a4b143
+                e = strv_env_merge(2, *environment, add);
a4b143
+                if (!e) {
a4b143
                         r = -ENOMEM;
a4b143
                         goto finish;
a4b143
                 }
a4b143
 
a4b143
-                *environment = env;
a4b143
+                strv_free(*environment);
a4b143
+                *environment = e;
a4b143
         }
a4b143
 
a4b143
         r = 0;
a4b143
 
a4b143
 finish:
a4b143
+        strv_free(add);
a4b143
+
a4b143
         for (i = 0; i < _VARIABLE_MAX; i++)
a4b143
                 free(variables[i]);
a4b143
 
a4b143
diff --git a/src/core/manager.c b/src/core/manager.c
a4b143
index dadbedd..30b49ff 100644
a4b143
--- a/src/core/manager.c
a4b143
+++ b/src/core/manager.c
a4b143
@@ -2666,14 +2666,16 @@ void manager_undo_generators(Manager *m) {
a4b143
 }
a4b143
 
a4b143
 int manager_environment_add(Manager *m, char **environment) {
a4b143
-
a4b143
         char **e = NULL;
a4b143
         assert(m);
a4b143
+
a4b143
         e = strv_env_merge(2, m->environment, environment);
a4b143
         if (!e)
a4b143
                 return -ENOMEM;
a4b143
+
a4b143
         strv_free(m->environment);
a4b143
         m->environment = e;
a4b143
+
a4b143
         return 0;
a4b143
 }
a4b143
 
a4b143
diff --git a/src/shared/strv.c b/src/shared/strv.c
a4b143
index 2df478f..adeee28 100644
a4b143
--- a/src/shared/strv.c
a4b143
+++ b/src/shared/strv.c
a4b143
@@ -424,21 +424,6 @@ fail:
a4b143
         return NULL;
a4b143
 }
a4b143
 
a4b143
-char **strv_appendf(char **l, const char *format, ...) {
a4b143
-        va_list ap;
a4b143
-        _cleanup_free_ char *s = NULL;
a4b143
-        int r;
a4b143
-
a4b143
-        va_start(ap, format);
a4b143
-        r = vasprintf(&s, format, ap);
a4b143
-        va_end(ap);
a4b143
-
a4b143
-        if (r < 0)
a4b143
-                return NULL;
a4b143
-
a4b143
-        return strv_append(l, s);
a4b143
-}
a4b143
-
a4b143
 int strv_push(char ***l, char *value) {
a4b143
         char **c;
a4b143
         unsigned n;
a4b143
diff --git a/src/shared/strv.h b/src/shared/strv.h
a4b143
index 4e80ea6..d1f2a0e 100644
a4b143
--- a/src/shared/strv.h
a4b143
+++ b/src/shared/strv.h
a4b143
@@ -42,7 +42,6 @@ unsigned strv_length(char * const *l) _pure_;
a4b143
 char **strv_merge(char **a, char **b);
a4b143
 char **strv_merge_concat(char **a, char **b, const char *suffix);
a4b143
 char **strv_append(char **l, const char *s);
a4b143
-char **strv_appendf(char **l, const char *format, ...) _printf_attr_(2, 3);
a4b143
 int strv_extend(char ***l, const char *value);
a4b143
 int strv_push(char ***l, char *value);
a4b143