@@ -380,7 +380,7 @@ static void child_err_spew(struct child_process *cmd, struct child_err *cerr)
380
380
set_error_routine (old_errfn );
381
381
}
382
382
383
- static void prepare_cmd (struct argv_array * out , const struct child_process * cmd )
383
+ static int prepare_cmd (struct argv_array * out , const struct child_process * cmd )
384
384
{
385
385
if (!cmd -> argv [0 ])
386
386
BUG ("command is empty" );
@@ -403,16 +403,22 @@ static void prepare_cmd(struct argv_array *out, const struct child_process *cmd)
403
403
/*
404
404
* If there are no '/' characters in the command then perform a path
405
405
* lookup and use the resolved path as the command to exec. If there
406
- * are no '/' characters or if the command wasn't found in the path,
407
- * have exec attempt to invoke the command directly.
406
+ * are '/' characters, we have exec attempt to invoke the command
407
+ * directly.
408
408
*/
409
409
if (!strchr (out -> argv [1 ], '/' )) {
410
410
char * program = locate_in_PATH (out -> argv [1 ]);
411
411
if (program ) {
412
412
free ((char * )out -> argv [1 ]);
413
413
out -> argv [1 ] = program ;
414
+ } else {
415
+ argv_array_clear (out );
416
+ errno = ENOENT ;
417
+ return -1 ;
414
418
}
415
419
}
420
+
421
+ return 0 ;
416
422
}
417
423
418
424
static char * * prep_childenv (const char * const * deltaenv )
@@ -719,6 +725,12 @@ int start_command(struct child_process *cmd)
719
725
struct child_err cerr ;
720
726
struct atfork_state as ;
721
727
728
+ if (prepare_cmd (& argv , cmd ) < 0 ) {
729
+ failed_errno = errno ;
730
+ cmd -> pid = -1 ;
731
+ goto end_of_spawn ;
732
+ }
733
+
722
734
if (pipe (notify_pipe ))
723
735
notify_pipe [0 ] = notify_pipe [1 ] = -1 ;
724
736
@@ -729,7 +741,6 @@ int start_command(struct child_process *cmd)
729
741
set_cloexec (null_fd );
730
742
}
731
743
732
- prepare_cmd (& argv , cmd );
733
744
childenv = prep_childenv (cmd -> env );
734
745
atfork_prepare (& as );
735
746
@@ -857,6 +868,8 @@ int start_command(struct child_process *cmd)
857
868
argv_array_clear (& argv );
858
869
free (childenv );
859
870
}
871
+ end_of_spawn :
872
+
860
873
#else
861
874
{
862
875
int fhin = 0 , fhout = 1 , fherr = 2 ;
0 commit comments