This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
dup2 fds can be bad.
authorJarkko Hietaniemi <jhi@iki.fi>
Tue, 23 Jun 2015 10:58:35 +0000 (06:58 -0400)
committerJarkko Hietaniemi <jhi@iki.fi>
Sat, 27 Jun 2015 03:09:42 +0000 (23:09 -0400)
Coverity CID 104812.

ext/POSIX/POSIX.xs

index ce4c12c..bd13d52 100644 (file)
@@ -3193,14 +3193,20 @@ dup2(fd1, fd2)
        int             fd1
        int             fd2
     CODE:
        int             fd1
        int             fd2
     CODE:
+       if (fd1 >= 0 && fd2 >= 0) {
 #ifdef WIN32
 #ifdef WIN32
-       /* RT #98912 - More Microsoft muppetry - failing to actually implemented
-          the well known documented POSIX behaviour for a POSIX API.
-          http://msdn.microsoft.com/en-us/library/8syseb29.aspx   */
-       RETVAL = dup2(fd1, fd2) == -1 ? -1 : fd2;
+            /* RT #98912 - More Microsoft muppetry - failing to
+               actually implemented the well known documented POSIX
+               behaviour for a POSIX API.
+               http://msdn.microsoft.com/en-us/library/8syseb29.aspx  */
+            RETVAL = dup2(fd1, fd2) == -1 ? -1 : fd2;
 #else
 #else
-       RETVAL = dup2(fd1, fd2);
+            RETVAL = dup2(fd1, fd2);
 #endif
 #endif
+        } else {
+            SETERRNO(EBADF,RMS_IFI);
+            RETVAL = -1;
+        }
     OUTPUT:
        RETVAL
 
     OUTPUT:
        RETVAL