This is the mail archive of the cygwin mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Fw: 1.5.11 bug in WEXITSTATUS() macro (wait.h)

On Sat, Sep 25, 2004 at 08:45:33PM +0200, Peter Dons Tychsen wrote:
>The WEXITSTATUS is a bit buggy. (wait.h)
>The macro extracts information gained from a call to waitpid() (and others).
>The information it extracts is the status of the completed process (8 bit
>signed value).
>The problem is that the macro does not cast the value to a signed integer
>(like other systems do), which can cause the value to be incorrectly
>interpreted (breaks some programs).
>The following fails:
>// Wait for processes to complete
>if(waitpid(pid, &status, 0) == pid)
>  // Check return value for failure (-1)
>  if(WEXITSTATUS(status) == -1)
>  {
>    /* We will never get here, as the macro returns 255 if the process
>exited with -1 */
>  }

Did you try this on linux?

I wrote the following simple test case (tm), (R), (C) and it does not
print a negative number.

#include <sys/wait.h>
#include <stdio.h>

main (int argc, char **argv)
  int pid = fork ();
  int *zero = 0;
  int status;
  if (!pid)
    if (argc > 1)
      *zero = 1; // boom
      exit (-1);
  if (waitpid(pid, &status, 0) == pid)
      if (WEXITSTATUS(status) == -1)
        puts ("it is negative");
        puts ("it is not negative");
      printf ("%d\n", WEXITSTATUS(status));

The reason for this is that the definition of WEXITSTATUS on linux is this:

#define __WEXITSTATUS(status) (((status) & 0xff00) >> 8)

which would not return a negative number.

The linux man page also has this to say:

              evaluates to the least significant eight bits of the return code
              of  the  child  which terminated, which may have been set as the
              argument to a call to exit() or _exit() or as the argument for a
              return  statement  in  the main program.  This macro can only be
              evaluated if WIFEXITED returned true.

So, it seems like if there is a problem with cygwin it is in the fact
that there is no assurance that only eight bits are being returned.

In short, I don't see how this could be a bug.
Christopher Faylor			spammer? ->
Cygwin Co-Project Leader
TimeSys, Inc.

Unsubscribe info:
Problem reports:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]