Skip to content

Commit bcd8e6d

Browse files
committed
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.
1 parent 6c08127 commit bcd8e6d

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

src/core/locale-setup.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "virt.h"
3030
#include "fileio.h"
3131
#include "strv.h"
32+
#include "env-util.h"
3233

3334
enum {
3435
/* We don't list LC_ALL here on purpose. People should be
@@ -69,7 +70,7 @@ static const char * const variable_names[_VARIABLE_MAX] = {
6970
};
7071

7172
int locale_setup(char ***environment) {
72-
char **env;
73+
char **add;
7374
char *variables[_VARIABLE_MAX] = {};
7475
int r = 0, i;
7576

@@ -119,22 +120,44 @@ int locale_setup(char ***environment) {
119120
log_warning("Failed to read /etc/locale.conf: %s", strerror(-r));
120121
}
121122

123+
add = NULL;
122124
for (i = 0; i < _VARIABLE_MAX; i++) {
125+
char *s;
126+
123127
if (!variables[i])
124128
continue;
125129

126-
env = strv_appendf(*environment, "%s=%s", variable_names[i], variables[i]);
127-
if (!env) {
130+
s = strjoin(variable_names[i], "=", variables[i], NULL);
131+
if (!s) {
132+
r = -ENOMEM;
133+
goto finish;
134+
}
135+
136+
if (strv_push(&add, s) < 0) {
137+
free(s);
138+
r = -ENOMEM;
139+
goto finish;
140+
}
141+
}
142+
143+
if (!strv_isempty(add)) {
144+
char **e;
145+
146+
e = strv_env_merge(2, *environment, add);
147+
if (!e) {
128148
r = -ENOMEM;
129149
goto finish;
130150
}
131151

132-
*environment = env;
152+
strv_free(*environment);
153+
*environment = e;
133154
}
134155

135156
r = 0;
136157

137158
finish:
159+
strv_free(add);
160+
138161
for (i = 0; i < _VARIABLE_MAX; i++)
139162
free(variables[i]);
140163

src/core/manager.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2665,14 +2665,16 @@ void manager_undo_generators(Manager *m) {
26652665
}
26662666

26672667
int manager_environment_add(Manager *m, char **environment) {
2668-
26692668
char **e = NULL;
26702669
assert(m);
2670+
26712671
e = strv_env_merge(2, m->environment, environment);
26722672
if (!e)
26732673
return -ENOMEM;
2674+
26742675
strv_free(m->environment);
26752676
m->environment = e;
2677+
26762678
return 0;
26772679
}
26782680

src/shared/strv.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -424,21 +424,6 @@ char **strv_append(char **l, const char *s) {
424424
return NULL;
425425
}
426426

427-
char **strv_appendf(char **l, const char *format, ...) {
428-
va_list ap;
429-
_cleanup_free_ char *s = NULL;
430-
int r;
431-
432-
va_start(ap, format);
433-
r = vasprintf(&s, format, ap);
434-
va_end(ap);
435-
436-
if (r < 0)
437-
return NULL;
438-
439-
return strv_append(l, s);
440-
}
441-
442427
int strv_push(char ***l, char *value) {
443428
char **c;
444429
unsigned n;

src/shared/strv.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ unsigned strv_length(char * const *l) _pure_;
4242
char **strv_merge(char **a, char **b);
4343
char **strv_merge_concat(char **a, char **b, const char *suffix);
4444
char **strv_append(char **l, const char *s);
45-
char **strv_appendf(char **l, const char *format, ...) _printf_attr_(2, 3);
4645
int strv_extend(char ***l, const char *value);
4746
int strv_push(char ***l, char *value);
4847

0 commit comments

Comments
 (0)