@@ -380,7 +380,7 @@ static void child_err_spew(struct child_process *cmd, struct child_err *cerr)
380380 set_error_routine (old_errfn );
381381}
382382
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 )
384384{
385385 if (!cmd -> argv [0 ])
386386 BUG ("command is empty" );
@@ -403,16 +403,22 @@ static void prepare_cmd(struct argv_array *out, const struct child_process *cmd)
403403 /*
404404 * If there are no '/' characters in the command then perform a path
405405 * 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.
408408 */
409409 if (!strchr (out -> argv [1 ], '/' )) {
410410 char * program = locate_in_PATH (out -> argv [1 ]);
411411 if (program ) {
412412 free ((char * )out -> argv [1 ]);
413413 out -> argv [1 ] = program ;
414+ } else {
415+ argv_array_clear (out );
416+ errno = ENOENT ;
417+ return -1 ;
414418 }
415419 }
420+
421+ return 0 ;
416422}
417423
418424static char * * prep_childenv (const char * const * deltaenv )
@@ -719,6 +725,12 @@ int start_command(struct child_process *cmd)
719725 struct child_err cerr ;
720726 struct atfork_state as ;
721727
728+ if (prepare_cmd (& argv , cmd ) < 0 ) {
729+ failed_errno = errno ;
730+ cmd -> pid = -1 ;
731+ goto end_of_spawn ;
732+ }
733+
722734 if (pipe (notify_pipe ))
723735 notify_pipe [0 ] = notify_pipe [1 ] = -1 ;
724736
@@ -729,7 +741,6 @@ int start_command(struct child_process *cmd)
729741 set_cloexec (null_fd );
730742 }
731743
732- prepare_cmd (& argv , cmd );
733744 childenv = prep_childenv (cmd -> env );
734745 atfork_prepare (& as );
735746
@@ -857,6 +868,8 @@ int start_command(struct child_process *cmd)
857868 argv_array_clear (& argv );
858869 free (childenv );
859870}
871+ end_of_spawn :
872+
860873#else
861874{
862875 int fhin = 0 , fhout = 1 , fherr = 2 ;
0 commit comments