local: fix memory leak when putting together locale settings
Also, we need to use proper strv_env_xyz() calls when putting together the environment array, since otherwise settings won't be properly overriden. And let's get rid of strv_appendf(), is overkill and there was only one user.
This commit is contained in:
parent
6c081276dc
commit
bcd8e6d1bd
|
@ -29,6 +29,7 @@
|
||||||
#include "virt.h"
|
#include "virt.h"
|
||||||
#include "fileio.h"
|
#include "fileio.h"
|
||||||
#include "strv.h"
|
#include "strv.h"
|
||||||
|
#include "env-util.h"
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
/* We don't list LC_ALL here on purpose. People should be
|
/* We don't list LC_ALL here on purpose. People should be
|
||||||
|
@ -69,7 +70,7 @@ static const char * const variable_names[_VARIABLE_MAX] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
int locale_setup(char ***environment) {
|
int locale_setup(char ***environment) {
|
||||||
char **env;
|
char **add;
|
||||||
char *variables[_VARIABLE_MAX] = {};
|
char *variables[_VARIABLE_MAX] = {};
|
||||||
int r = 0, i;
|
int r = 0, i;
|
||||||
|
|
||||||
|
@ -119,22 +120,44 @@ int locale_setup(char ***environment) {
|
||||||
log_warning("Failed to read /etc/locale.conf: %s", strerror(-r));
|
log_warning("Failed to read /etc/locale.conf: %s", strerror(-r));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
add = NULL;
|
||||||
for (i = 0; i < _VARIABLE_MAX; i++) {
|
for (i = 0; i < _VARIABLE_MAX; i++) {
|
||||||
|
char *s;
|
||||||
|
|
||||||
if (!variables[i])
|
if (!variables[i])
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
env = strv_appendf(*environment, "%s=%s", variable_names[i], variables[i]);
|
s = strjoin(variable_names[i], "=", variables[i], NULL);
|
||||||
if (!env) {
|
if (!s) {
|
||||||
r = -ENOMEM;
|
r = -ENOMEM;
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
*environment = env;
|
if (strv_push(&add, s) < 0) {
|
||||||
|
free(s);
|
||||||
|
r = -ENOMEM;
|
||||||
|
goto finish;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!strv_isempty(add)) {
|
||||||
|
char **e;
|
||||||
|
|
||||||
|
e = strv_env_merge(2, *environment, add);
|
||||||
|
if (!e) {
|
||||||
|
r = -ENOMEM;
|
||||||
|
goto finish;
|
||||||
|
}
|
||||||
|
|
||||||
|
strv_free(*environment);
|
||||||
|
*environment = e;
|
||||||
}
|
}
|
||||||
|
|
||||||
r = 0;
|
r = 0;
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
|
strv_free(add);
|
||||||
|
|
||||||
for (i = 0; i < _VARIABLE_MAX; i++)
|
for (i = 0; i < _VARIABLE_MAX; i++)
|
||||||
free(variables[i]);
|
free(variables[i]);
|
||||||
|
|
||||||
|
|
|
@ -2665,14 +2665,16 @@ void manager_undo_generators(Manager *m) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int manager_environment_add(Manager *m, char **environment) {
|
int manager_environment_add(Manager *m, char **environment) {
|
||||||
|
|
||||||
char **e = NULL;
|
char **e = NULL;
|
||||||
assert(m);
|
assert(m);
|
||||||
|
|
||||||
e = strv_env_merge(2, m->environment, environment);
|
e = strv_env_merge(2, m->environment, environment);
|
||||||
if (!e)
|
if (!e)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
strv_free(m->environment);
|
strv_free(m->environment);
|
||||||
m->environment = e;
|
m->environment = e;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -424,21 +424,6 @@ fail:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char **strv_appendf(char **l, const char *format, ...) {
|
|
||||||
va_list ap;
|
|
||||||
_cleanup_free_ char *s = NULL;
|
|
||||||
int r;
|
|
||||||
|
|
||||||
va_start(ap, format);
|
|
||||||
r = vasprintf(&s, format, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
if (r < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
return strv_append(l, s);
|
|
||||||
}
|
|
||||||
|
|
||||||
int strv_push(char ***l, char *value) {
|
int strv_push(char ***l, char *value) {
|
||||||
char **c;
|
char **c;
|
||||||
unsigned n;
|
unsigned n;
|
||||||
|
|
|
@ -42,7 +42,6 @@ unsigned strv_length(char * const *l) _pure_;
|
||||||
char **strv_merge(char **a, char **b);
|
char **strv_merge(char **a, char **b);
|
||||||
char **strv_merge_concat(char **a, char **b, const char *suffix);
|
char **strv_merge_concat(char **a, char **b, const char *suffix);
|
||||||
char **strv_append(char **l, const char *s);
|
char **strv_append(char **l, const char *s);
|
||||||
char **strv_appendf(char **l, const char *format, ...) _printf_attr_(2, 3);
|
|
||||||
int strv_extend(char ***l, const char *value);
|
int strv_extend(char ***l, const char *value);
|
||||||
int strv_push(char ***l, char *value);
|
int strv_push(char ***l, char *value);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue