Skip to content

Commit

Permalink
sub-process: do not borrow cmd pointer from caller
Browse files Browse the repository at this point in the history
Teach subprocess_start() to use a copy of the passed `cmd` string
rather than borrowing the buffer from the caller.

Some callers of subprocess_start() pass the value returned from
find_hook() which points to a static buffer and therefore is only
good until the next call to find_hook().  This could cause problems
for the long-running background processes managed by sub-process.c
where later calls to subprocess_find_entry() to get an existing
process will fail.  This could cause more than 1 long-running
process to be created.

TODO Need to confirm, but if only read_object_hook() uses
TODO subprocess_start() in this manner, we could drop this
TODO commit when we drop support for read_object_hook().

Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
  • Loading branch information
jeffhostetler authored and dscho committed Aug 16, 2023
1 parent 34caf2a commit 817cf58
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion sub-process.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, co
int err;
struct child_process *process;

entry->cmd = cmd;
// BUGBUG most callers to subprocess_start() pass in "cmd" the value
// BUGBUG of find_hook() which returns a static buffer (that's only
// BUGBUG good until the next call to find_hook()).
// BUGFIX Defer assignment until we copy the string in our argv.
// entry->cmd = cmd;

process = &entry->process;

child_process_init(process);
Expand All @@ -93,6 +98,8 @@ int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, co
process->clean_on_exit_handler = subprocess_exit_handler;
process->trace2_child_class = "subprocess";

entry->cmd = process->args.v[0];

err = start_command(process);
if (err) {
error("cannot fork to run subprocess '%s'", cmd);
Expand Down

0 comments on commit 817cf58

Please sign in to comment.