[MinGW-Notify] [mingw] #42561: MSVCRT.DLL implementation of POSIX dup2() function does not conform to POSIX.1

Back to archive index
MinGW Notification List mingw****@lists*****
Wed Jun 23 07:34:59 JST 2021


#42561: MSVCRT.DLL implementation of POSIX dup2() function does not conform to POSIX.1

  Open Date: 2021-06-22 23:34
Last Update: 2021-06-22 23:34

URL for this Ticket:
    https://osdn.net//projects/mingw/ticket/42561
RSS feed for this Ticket:
    https://osdn.net/ticket/ticket_rss.php?group_id=3917&tid=42561

---------------------------------------------------------------------

Last Changes/Comment on this Ticket:
2021-06-22 23:34 Updated by: keith
 * New Ticket "MSVCRT.DLL implementation of POSIX dup2() function does not conform to POSIX.1" created


---------------------------------------------------------------------
Ticket Status:

      Reporter: keith
         Owner: (None)
          Type: Issues
        Status: Open
      Priority: 5 - Medium
     MileStone: (None)
     Component: WSL
      Severity: 5 - Medium
    Resolution: None
---------------------------------------------------------------------

Ticket details:

I've been aware of this non-conformity for several years, yet it continues to trip me up, on occasion.  Consider the code fragment:
 int fd = open( "foo.txt", O_CREAT | O_EXCL, S_IREAD | S_IWRITE ); if( dup2( fd, STDOUT_FILENO ) != STDOUT_FILENO )     perror( "open" );On a POSIX.1 conforming platform, (with added support for the BSD standard S_IREAD and S_IWRITE macros), assuming that "foo.txt" is successfully opened, and bound to STDOUT_FILENO by the dup2() call, the condition dup2( fd, STDOUT_FILENO ) == STDOUT_FILENO will evaluate as TRUE, and the perror() call will not be invoked.  However, on Windows, the same code, (assuming that errno is zero, immediately prior to its execution), will likely result in output (from perror(), which is invoked) similar to:
open: Success.
The reason for this disparity, given a function prototype of int dup2( int fd1, int fd2 ), is that, whereas POSIX.1 stipulates that the return value, following a successful invocation, shall be equal to fd2, the Microsoft implementation always returns zero, on success, regardless of the value of fd2.
To be fair to Microsoft, although their documentation does describe dup2() as a POSIX function (in name only), it indicates that it is a deprecated ("because [it doesn't] follow the Standard C rules for implementation-specific names" ... a rationale which would seem to not be endorsed by ISO/IEC, since dup2() is ratified as an international standard name, within ISO/IEC 9945, as it is in IEEE 1003.1-2017) alias for Microsoft's non-standard _dup2(), for which the documentation does indicate the non-conformity w.r.t. the return value stipulated by POSIX.1; however, given that this catches me out, every time I have occasion to use dup2(), and I don't think it is unreasonable to expect POSIX.1 conformity, when I call any function by its POSIX.1 name, I really would like to see this fixed in the MinGW implementation.

-- 
Ticket information of MinGW - Minimalist GNU for Windows project
MinGW - Minimalist GNU for Windows Project is hosted on OSDN

Project URL: https://osdn.net/projects/mingw/
OSDN: https://osdn.net

URL for this Ticket:
    https://osdn.net/projects/mingw/ticket/42561
RSS feed for this Ticket:
    https://osdn.net/ticket/ticket_rss.php?group_id=3917&tid=42561



More information about the MinGW-Notify mailing list
Back to archive index