Fix #384: reassemble value fragments after wordexp() before setenv()

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2023-11-12 08:50:12 +01:00
1 parent 60bf858302
commit 0ebdd32f6b
1 file changed
+16 -7
+16 -7
View File
@@ -394,11 +394,8 @@ static int redirect(svc_t *svc)
*/ */
static void source_env(svc_t *svc) static void source_env(svc_t *svc)
{ {
char buf[LINE_SIZE]; char *buf, *val, *line, *fn;
char *line;
size_t len;
FILE *fp; FILE *fp;
char *fn;
fn = svc_getenv(svc); fn = svc_getenv(svc);
if (!fn) if (!fn)
@@ -409,12 +406,19 @@ static void source_env(svc_t *svc)
if (!fp) if (!fp)
return; return;
buf = alloca(LINE_SIZE);
val = alloca(LINE_SIZE);
if (!buf || !val) {
warn("Failed allocating temporary env buffer");
return;
}
line = buf; line = buf;
len = sizeof(buf); while (fgets(line, LINE_SIZE, fp)) {
while (fgets(line, len, fp)) {
char *key = chomp(line); char *key = chomp(line);
wordexp_t we = { 0 }; wordexp_t we = { 0 };
char *value, *end; char *value, *end;
size_t i;
/* skip any leading whitespace */ /* skip any leading whitespace */
while (isspace(*key)) while (isspace(*key))
@@ -469,7 +473,12 @@ static void source_env(svc_t *svc)
switch (wordexp(value, &we, 0)) { switch (wordexp(value, &we, 0)) {
case 0: case 0:
setenv(key, we.we_wordv[0], 1); for (i = 0, *val = 0; i < we.we_wordc; i++) {
if (i > 0)
strlcat(val, " ", LINE_SIZE);
strlcat(val, we.we_wordv[i], LINE_SIZE);
}
setenv(key, val, 1);
wordfree(&we); wordfree(&we);
break; break;