Skip to content

Commit b8f5242

Browse files
committed
Merge branch 'rs/daemon-plug-child-leak' into maint
"git daemon" uses "run_command()" without "finish_command()", so it needs to release resources itself, which it forgot to do. * rs/daemon-plug-child-leak: daemon: plug memory leak run-command: factor out child_process_clear()
2 parents db43891 + b1b49ff commit b8f5242

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

Documentation/technical/api-run-command.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ Functions
4646
The argument dir corresponds the member .dir. The argument env
4747
corresponds to the member .env.
4848

49+
`child_process_clear`::
50+
51+
Release the memory associated with the struct child_process.
52+
Most users of the run-command API don't need to call this
53+
function explicitly because `start_command` invokes it on
54+
failure and `finish_command` calls it automatically already.
55+
4956
The functions above do the following:
5057

5158
. If a system call failed, errno is set and -1 is returned. A diagnostic

daemon.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ static void check_dead_children(void)
802802
/* remove the child */
803803
*cradle = blanket->next;
804804
live_children--;
805+
child_process_clear(&blanket->cld);
805806
free(blanket);
806807
} else
807808
cradle = &blanket->next;

run-command.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ void child_process_init(struct child_process *child)
1111
argv_array_init(&child->env_array);
1212
}
1313

14+
void child_process_clear(struct child_process *child)
15+
{
16+
argv_array_clear(&child->args);
17+
argv_array_clear(&child->env_array);
18+
}
19+
1420
struct child_to_clean {
1521
pid_t pid;
1622
struct child_to_clean *next;
@@ -327,8 +333,7 @@ int start_command(struct child_process *cmd)
327333
fail_pipe:
328334
error("cannot create %s pipe for %s: %s",
329335
str, cmd->argv[0], strerror(failed_errno));
330-
argv_array_clear(&cmd->args);
331-
argv_array_clear(&cmd->env_array);
336+
child_process_clear(cmd);
332337
errno = failed_errno;
333338
return -1;
334339
}
@@ -513,8 +518,7 @@ int start_command(struct child_process *cmd)
513518
close_pair(fderr);
514519
else if (cmd->err)
515520
close(cmd->err);
516-
argv_array_clear(&cmd->args);
517-
argv_array_clear(&cmd->env_array);
521+
child_process_clear(cmd);
518522
errno = failed_errno;
519523
return -1;
520524
}
@@ -540,8 +544,7 @@ int start_command(struct child_process *cmd)
540544
int finish_command(struct child_process *cmd)
541545
{
542546
int ret = wait_or_whine(cmd->pid, cmd->argv[0], 0);
543-
argv_array_clear(&cmd->args);
544-
argv_array_clear(&cmd->env_array);
547+
child_process_clear(cmd);
545548
return ret;
546549
}
547550

run-command.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct child_process {
4747

4848
#define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT, ARGV_ARRAY_INIT }
4949
void child_process_init(struct child_process *);
50+
void child_process_clear(struct child_process *);
5051

5152
int start_command(struct child_process *);
5253
int finish_command(struct child_process *);

0 commit comments

Comments
 (0)