threads / patch / 30154

patchcompat/mingw.[ch]: Change return type of exec functions to int

Subject: [PATCH] compat/mingw.[ch]: Change return type of exec functions to int

## tl;dr

8 messages between Apr 5, 2012 and Apr 6, 2012. Diffs are folded; open one to read it.

replies: 7people: 4as markdown or json

Ramsay Jones· Apr 5, 2012, 17:48 UTC · lore

The POSIX standard specifies a return type of int for all six exec functions. In addition, all exec functions return -1 on error, and simply do not return on success. However, the current emulation of the exec functions on mingw are declared with a void return type.

This would cause a problem should any code attempt to call the exec function in a non-void context. In particular, if an exec function were used in a conditional it would fail to compile.

In order to improve the fidelity of the emulation, we change the return type of the mingw_execv[p] functions to int and return -1 on error.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
---
Hi Jeff,

Since commit b0984f82 ("run-command: treat inaccessible directories as ENOENT", 30-03-2012), the mingw build fails as follows:

        CC run-command.o
    run-command.c: In function 'sane_execvp':
    run-command.c:124: error: invalid use of void expression
    make: *** [run-command.o] Error 1

My first reaction was to simply remove the conditional since, if execvp() returns at all, the result will always be -1 and so the condition will always be false. ie. the conditional is pointless.

However, I found the incorrect return type of the mingw_execv[p]() to be a gratuitous incompatibility, so ... :-P

Could you (or Junio?) add this patch prior to your patch on this branch ? Thanks in advance!

I will let you decide if you want to remove the conditional as well ...

ATB, Ramsay Jones

 compat/mingw.c |    6 ++++--
 compat/mingw.h |    4 ++--
 2 files changed, 6 insertions(+), 4 deletions(-)
Show changes to 2 files +6 −4

compat/mingw.c, compat/mingw.h

diff --git a/compat/mingw.c b/compat/mingw.c
index a0ac487..afc892d 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -1003,7 +1003,7 @@ static void mingw_execve(const char *cmd, char *const *argv, char *const *env)
 	}
 }
 
-void mingw_execvp(const char *cmd, char *const *argv)
+int mingw_execvp(const char *cmd, char *const *argv)
 {
 	char **path = get_path_split();
 	char *prog = path_lookup(cmd, path, 0);
@@ -1015,11 +1015,13 @@ void mingw_execvp(const char *cmd, char *const *argv)
 		errno = ENOENT;
 
 	free_path_split(path);
+	return -1;
 }
 
-void mingw_execv(const char *cmd, char *const *argv)
+int mingw_execv(const char *cmd, char *const *argv)
 {
 	mingw_execve(cmd, argv, environ);
+	return -1;
 }
 
 int mingw_kill(pid_t pid, int sig)
diff --git a/compat/mingw.h b/compat/mingw.h
index 0ff1e04..ef5b150 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -274,9 +274,9 @@ int mingw_utime(const char *file_name, const struct utimbuf *times);
 pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,
 		     const char *dir,
 		     int fhin, int fhout, int fherr);
-void mingw_execvp(const char *cmd, char *const *argv);
+int mingw_execvp(const char *cmd, char *const *argv);
 #define execvp mingw_execvp
-void mingw_execv(const char *cmd, char *const *argv);
+int mingw_execv(const char *cmd, char *const *argv);
 #define execv mingw_execv
 
 static inline unsigned int git_ntohl(unsigned int x)
-- 
1.7.9
Jonathan Nieder· Apr 5, 2012, 18:16 UTC · re: Ramsay Jones · lore

Re: [PATCH] compat/mingw.[ch]: Change return type of exec functions to int

Ramsay Jones wrote:
Show 11 quoted lines
>         CC run-command.o
>     run-command.c: In function 'sane_execvp':
>     run-command.c:124: error: invalid use of void expression
>     make: *** [run-command.o] Error 1
>
> My first reaction was to simply remove the conditional since, if execvp()
> returns at all, the result will always be -1 and so the condition will
> always be false. ie. the conditional is pointless.
>
> However, I found the incorrect return type of the mingw_execv[p]() to be
> a gratuitous incompatibility, so ... :-P

My bad. I agree that in addition to making the return type fix, squashing the following into jk/run-command-eacces would be a good idea.

Show changes to diff +1 −2
diff --git i/run-command.c w/run-command.c
index 04f0190d..fcd7e192 100644
--- i/run-command.c
+++ w/run-command.c
@@ -59,8 +59,7 @@ static int exists_in_PATH(const char *file)
 
 int sane_execvp(const char *file, char * const argv[])
 {
-	if (!execvp(file, argv))
-		return 0;
+	execvp(file, argv);
 
 	/*
 	 * When a command can't be found because one of the directories
Jeff King· Apr 5, 2012, 22:06 UTC · re: Jonathan Nieder · lore

Re: [PATCH] compat/mingw.[ch]: Change return type of exec functions to int

On Thu, Apr 05, 2012 at 01:16:01PM -0500, Jonathan Nieder wrote:
Show 13 quoted lines
> Ramsay Jones wrote:
> 
> >         CC run-command.o
> >     run-command.c: In function 'sane_execvp':
> >     run-command.c:124: error: invalid use of void expression
> >     make: *** [run-command.o] Error 1
> >
> > My first reaction was to simply remove the conditional since, if execvp()
> > returns at all, the result will always be -1 and so the condition will
> > always be false. ie. the conditional is pointless.
> >
> > However, I found the incorrect return type of the mingw_execv[p]() to be
> > a gratuitous incompatibility, so ... :-P

Yeah, it is just asking for trouble to #define a new execvp that does not match the declaration of the existing one, so I think Ramsay's patch is good.

Show 15 quoted lines
> My bad.  I agree that in addition to making the return type fix,
> squashing the following into jk/run-command-eacces would be a good
> idea.
> 
> diff --git i/run-command.c w/run-command.c
> index 04f0190d..fcd7e192 100644
> --- i/run-command.c
> +++ w/run-command.c
> @@ -59,8 +59,7 @@ static int exists_in_PATH(const char *file)
>  
>  int sane_execvp(const char *file, char * const argv[])
>  {
> -	if (!execvp(file, argv))
> -		return 0;
> +	execvp(file, argv);

I don't have a strong opinion. The "return 0" is a little misleading, since it will never be called, but I think we should at least have a comment like:

Show changes to run-command.c +5 −2
diff --git a/run-command.c b/run-command.c
index 7123436..5b6a368 100644
--- a/run-command.c
+++ b/run-command.c
@@ -117,8 +117,11 @@ static int exists_in_PATH(const char *file)
 
 int sane_execvp(const char *file, char * const argv[])
 {
-	if (!execvp(file, argv))
-		return 0;
+	/*
+	 * No need to check the return value; if it returns at all, an error
+	 * occurred.
+	 */
+	execvp(file, argv);
 
 	/*
 	 * When a command can't be found because one of the directories
Jonathan Nieder· Apr 5, 2012, 22:34 UTC · re: Jeff King · lore

Re: [PATCH] compat/mingw.[ch]: Change return type of exec functions to int

Jeff King wrote:
> I don't have a strong opinion. The "return 0" is a little misleading,
> since it will never be called, but I think we should at least have a
> comment like:
[...]
Show 14 quoted lines
> --- a/run-command.c
> +++ b/run-command.c
> @@ -117,8 +117,11 @@ static int exists_in_PATH(const char *file)
>  
>  int sane_execvp(const char *file, char * const argv[])
>  {
> -	if (!execvp(file, argv))
> -		return 0;
> +	/*
> +	 * No need to check the return value; if it returns at all, an error
> +	 * occurred.
> +	 */
> +	execvp(file, argv);
>  

I'd rather have the "return 0" then. Such a comment that focuses on C library details rather than providing additional information for understanding the sane_execvp function is distracting.

Jeff King· Apr 6, 2012, 00:24 UTC · re: Jonathan Nieder · lore

Re: [PATCH] compat/mingw.[ch]: Change return type of exec functions to int

On Thu, Apr 05, 2012 at 05:34:36PM -0500, Jonathan Nieder wrote:
Show 18 quoted lines
> > --- a/run-command.c
> > +++ b/run-command.c
> > @@ -117,8 +117,11 @@ static int exists_in_PATH(const char *file)
> >  
> >  int sane_execvp(const char *file, char * const argv[])
> >  {
> > -	if (!execvp(file, argv))
> > -		return 0;
> > +	/*
> > +	 * No need to check the return value; if it returns at all, an error
> > +	 * occurred.
> > +	 */
> > +	execvp(file, argv);
> >  
> 
> I'd rather have the "return 0" then.  Such a comment that focuses on C
> library details rather than providing additional information for
> understanding the sane_execvp function is distracting.

I think both (attempt to) document the same thing: that if we get past execvp, we know are in error-checking mode. Which is not explicitly said anywhere. So maybe:

Show changes to run-command.c +4 −2
diff --git a/run-command.c b/run-command.c
index 7123436..e6ece79 100644
--- a/run-command.c
+++ b/run-command.c
@@ -117,10 +117,12 @@ static int exists_in_PATH(const char *file)
 
 int sane_execvp(const char *file, char * const argv[])
 {
-	if (!execvp(file, argv))
-		return 0;
+	execvp(file, argv);
 
 	/*
+	 * If we are still running, we know an error occurred; let's try to
+	 * diagnose it more specifically.
+	 *
 	 * When a command can't be found because one of the directories
 	 * listed in $PATH is unsearchable, execvp reports EACCES, but
 	 * careful usability testing (read: analysis of occasional bug
Jonathan Nieder· Apr 6, 2012, 00:42 UTC · re: Jeff King · lore

Re: [PATCH] compat/mingw.[ch]: Change return type of exec functions to int

Jeff King wrote:
Show 21 quoted lines
> I think both (attempt to) document the same thing: that if we get past
> execvp, we know are in error-checking mode. Which is not explicitly said
> anywhere. So maybe:
>
> diff --git a/run-command.c b/run-command.c
> index 7123436..e6ece79 100644
> --- a/run-command.c
> +++ b/run-command.c
> @@ -117,10 +117,12 @@ static int exists_in_PATH(const char *file)
>  
>  int sane_execvp(const char *file, char * const argv[])
>  {
> -	if (!execvp(file, argv))
> -		return 0;
> +	execvp(file, argv);
>  
>  	/*
> +	 * If we are still running, we know an error occurred; let's try to
> +	 * diagnose it more specifically.
> +	 *
>  	 * When a command can't be found because one of the directories
Looks fine to me. :)

Thanks for your patience. Jonathan

Jeff King· Apr 6, 2012, 00:44 UTC · re: Jonathan Nieder · lore

Re: [PATCH] compat/mingw.[ch]: Change return type of exec functions to int

On Thu, Apr 05, 2012 at 07:42:26PM -0500, Jonathan Nieder wrote:
Show 7 quoted lines
> >  	/*
> > +	 * If we are still running, we know an error occurred; let's try to
> > +	 * diagnose it more specifically.
> > +	 *
> >  	 * When a command can't be found because one of the directories
> 
> Looks fine to me. :)

Thanks. Junio, do you mind squashing this onto the tip of jk/run-command-eaccess? Patch repeated below for your convenience.

This should fix Ramsay's problem, but I think his patch is worth applying, anyway.

---
Show changes to run-command.c +4 −2
diff --git a/run-command.c b/run-command.c
index 7123436..e6ece79 100644
--- a/run-command.c
+++ b/run-command.c
@@ -117,10 +117,12 @@ static int exists_in_PATH(const char *file)
 
 int sane_execvp(const char *file, char * const argv[])
 {
-	if (!execvp(file, argv))
-		return 0;
+	execvp(file, argv);
 
 	/*
+	 * If we are still running, we know an error occurred; let's try to
+	 * diagnose it more specifically.
+	 *
 	 * When a command can't be found because one of the directories
 	 * listed in $PATH is unsearchable, execvp reports EACCES, but
 	 * careful usability testing (read: analysis of occasional bug
Junio C Hamano· Apr 6, 2012, 01:00 UTC · re: Jeff King · lore

Re: [PATCH] compat/mingw.[ch]: Change return type of exec functions to int

Jeff King <peff@peff.net> writes:
Show 12 quoted lines
> On Thu, Apr 05, 2012 at 07:42:26PM -0500, Jonathan Nieder wrote:
>
>> >  	/*
>> > +	 * If we are still running, we know an error occurred; let's try to
>> > +	 * diagnose it more specifically.
>> > +	 *
>> >  	 * When a command can't be found because one of the directories
>> 
>> Looks fine to me. :)
>
> Thanks. Junio, do you mind squashing this onto the tip of
> jk/run-command-eaccess? Patch repeated below for your convenience.
Thanks. I've pushed out today's round already, so maybe later.

← back to recent threads