Skip to content

Commit 2a73b3d

Browse files
stefanbellergitster
authored andcommitted
run-command: do not pass child process data into callbacks
The expected way to pass data into the callback is to pass them via the customizable callback pointer. The error reporting in default_{start_failure, task_finished} is not user friendly enough, that we want to encourage using the child data for such purposes. Furthermore the struct child data is cleaned by the run-command API, before we access them in the callbacks, leading to use-after-free situations. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 62104ba commit 2a73b3d

File tree

4 files changed

+9
-32
lines changed

4 files changed

+9
-32
lines changed

run-command.c

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -902,35 +902,18 @@ struct parallel_processes {
902902
struct strbuf buffered_output; /* of finished children */
903903
};
904904

905-
static int default_start_failure(struct child_process *cp,
906-
struct strbuf *err,
905+
static int default_start_failure(struct strbuf *err,
907906
void *pp_cb,
908907
void *pp_task_cb)
909908
{
910-
int i;
911-
912-
strbuf_addstr(err, "Starting a child failed:");
913-
for (i = 0; cp->argv[i]; i++)
914-
strbuf_addf(err, " %s", cp->argv[i]);
915-
916909
return 0;
917910
}
918911

919912
static int default_task_finished(int result,
920-
struct child_process *cp,
921913
struct strbuf *err,
922914
void *pp_cb,
923915
void *pp_task_cb)
924916
{
925-
int i;
926-
927-
if (!result)
928-
return 0;
929-
930-
strbuf_addf(err, "A child failed with return code %d:", result);
931-
for (i = 0; cp->argv[i]; i++)
932-
strbuf_addf(err, " %s", cp->argv[i]);
933-
934917
return 0;
935918
}
936919

@@ -1048,8 +1031,7 @@ static int pp_start_one(struct parallel_processes *pp)
10481031
pp->children[i].process.no_stdin = 1;
10491032

10501033
if (start_command(&pp->children[i].process)) {
1051-
code = pp->start_failure(&pp->children[i].process,
1052-
&pp->children[i].err,
1034+
code = pp->start_failure(&pp->children[i].err,
10531035
pp->data,
10541036
&pp->children[i].data);
10551037
strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
@@ -1117,7 +1099,7 @@ static int pp_collect_finished(struct parallel_processes *pp)
11171099

11181100
code = finish_command(&pp->children[i].process);
11191101

1120-
code = pp->task_finished(code, &pp->children[i].process,
1102+
code = pp->task_finished(code,
11211103
&pp->children[i].err, pp->data,
11221104
&pp->children[i].data);
11231105

run-command.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ typedef int (*get_next_task_fn)(struct child_process *cp,
158158
* To send a signal to other child processes for abortion, return
159159
* the negative signal number.
160160
*/
161-
typedef int (*start_failure_fn)(struct child_process *cp,
162-
struct strbuf *err,
161+
typedef int (*start_failure_fn)(struct strbuf *err,
163162
void *pp_cb,
164163
void *pp_task_cb);
165164

@@ -178,7 +177,6 @@ typedef int (*start_failure_fn)(struct child_process *cp,
178177
* the negative signal number.
179178
*/
180179
typedef int (*task_finished_fn)(int result,
181-
struct child_process *cp,
182180
struct strbuf *err,
183181
void *pp_cb,
184182
void *pp_task_cb);
@@ -192,9 +190,8 @@ typedef int (*task_finished_fn)(int result,
192190
* (both stdout and stderr) is routed to stderr in a manner that output
193191
* from different tasks does not interleave.
194192
*
195-
* If start_failure_fn or task_finished_fn are NULL, default handlers
196-
* will be used. The default handlers will print an error message on
197-
* error without issuing an emergency stop.
193+
* start_failure_fn and task_finished_fn can be NULL to omit any
194+
* special handling.
198195
*/
199196
int run_processes_parallel(int n,
200197
get_next_task_fn,

submodule.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,7 @@ static int get_next_submodule(struct child_process *cp,
705705
return 0;
706706
}
707707

708-
static int fetch_start_failure(struct child_process *cp,
709-
struct strbuf *err,
708+
static int fetch_start_failure(struct strbuf *err,
710709
void *cb, void *task_cb)
711710
{
712711
struct submodule_parallel_fetch *spf = cb;
@@ -716,8 +715,8 @@ static int fetch_start_failure(struct child_process *cp,
716715
return 0;
717716
}
718717

719-
static int fetch_finish(int retvalue, struct child_process *cp,
720-
struct strbuf *err, void *cb, void *task_cb)
718+
static int fetch_finish(int retvalue, struct strbuf *err,
719+
void *cb, void *task_cb)
721720
{
722721
struct submodule_parallel_fetch *spf = cb;
723722

test-run-command.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ static int no_job(struct child_process *cp,
4141
}
4242

4343
static int task_finished(int result,
44-
struct child_process *cp,
4544
struct strbuf *err,
4645
void *pp_cb,
4746
void *pp_task_cb)

0 commit comments

Comments
 (0)