X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/6ed60307ec0bd14728ad72c8f6fb4c27112cbff6..fa9804ae636b8a12f77d0e537f628658d44ea189:/dist/IO/IO.xs diff --git a/dist/IO/IO.xs b/dist/IO/IO.xs index 9056cb6..4b13feb 100644 --- a/dist/IO/IO.xs +++ b/dist/IO/IO.xs @@ -102,13 +102,19 @@ not_here(const char *s) static int io_blocking(pTHX_ InputStream f, int block) { + int fd = -1; #if defined(HAS_FCNTL) int RETVAL; - if(!f) { + if (!f) { errno = EBADF; return -1; } - RETVAL = fcntl(PerlIO_fileno(f), F_GETFL, 0); + fd = PerlIO_fileno(f); + if (fd < 0) { + errno = EBADF; + return -1; + } + RETVAL = fcntl(fd, F_GETFL, 0); if (RETVAL >= 0) { int mode = RETVAL; int newmode = mode; @@ -143,7 +149,7 @@ io_blocking(pTHX_ InputStream f, int block) } #endif if (newmode != mode) { - const int ret = fcntl(PerlIO_fileno(f),F_SETFL,newmode); + const int ret = fcntl(fd, F_SETFL, newmode); if (ret < 0) RETVAL = ret; } @@ -154,7 +160,7 @@ io_blocking(pTHX_ InputStream f, int block) if (block >= 0) { unsigned long flags = !block; /* ioctl claims to take char* but really needs a u_long sized buffer */ - const int ret = ioctl(PerlIO_fileno(f), FIONBIO, (char*)&flags); + const int ret = ioctl(fd, FIONBIO, (char*)&flags); if (ret != 0) return -1; /* Win32 has no way to get the current blocking status of a socket. @@ -524,9 +530,15 @@ fsync(arg) handle = IoOFP(sv_2io(arg)); if (!handle) handle = IoIFP(sv_2io(arg)); - if(handle) - RETVAL = fsync(PerlIO_fileno(handle)); - else { + if (handle) { + int fd = PerlIO_fileno(handle); + if (fd >= 0) { + RETVAL = fsync(fd); + } else { + RETVAL = -1; + errno = EBADF; + } + } else { RETVAL = -1; errno = EINVAL; } @@ -554,14 +566,23 @@ sockatmark (sock) InputStream sock PROTOTYPE: $ PREINIT: - int fd; + int fd = PerlIO_fileno(sock); CODE: { - fd = PerlIO_fileno(sock); #ifdef HAS_SOCKATMARK - RETVAL = sockatmark(fd); + if (fd < 0) { + errno = EBADF; + RETVAL = -1; + } else { + RETVAL = sockatmark(fd); + } #else { + if (fd < 0) { + errno = EBADF; + RETVAL = -1; + } + else { int flag = 0; # ifdef SIOCATMARK # if defined(NETWARE) || defined(WIN32) @@ -575,6 +596,7 @@ sockatmark (sock) # endif RETVAL = flag; } + } #endif } OUTPUT: