f6ea51
From 8c7182b26a43f14cd8afbfbe4448cbbd691c3609 Mon Sep 17 00:00:00 2001
f6ea51
From: Zefram <zefram@fysh.org>
f6ea51
Date: Wed, 15 Nov 2017 08:11:37 +0000
f6ea51
Subject: [PATCH] set $! when statting a closed filehandle
f6ea51
MIME-Version: 1.0
f6ea51
Content-Type: text/plain; charset=UTF-8
f6ea51
Content-Transfer-Encoding: 8bit
f6ea51
f6ea51
When a stat fails because it's on a closed or otherwise invalid
f6ea51
filehandle, $! was often not being set, depending on the operation
f6ea51
and the nature of the invalidity.  Consistently set it to EBADF.
f6ea51
Fixes [perl #108288].
f6ea51
f6ea51
Petr Písař: Ported to 5.26.1.
f6ea51
f6ea51
Signed-off-by: Petr Písař <ppisar@redhat.com>
f6ea51
---
f6ea51
 MANIFEST           |  1 +
f6ea51
 doio.c             | 10 +++++++++-
f6ea51
 pp_sys.c           | 22 ++++++++++++---------
f6ea51
 t/op/stat_errors.t | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
f6ea51
 4 files changed, 80 insertions(+), 10 deletions(-)
f6ea51
 create mode 100644 t/op/stat_errors.t
f6ea51
f6ea51
diff --git a/MANIFEST b/MANIFEST
f6ea51
index fcbf5cc..996759e 100644
f6ea51
--- a/MANIFEST
f6ea51
+++ b/MANIFEST
f6ea51
@@ -5670,6 +5670,7 @@ t/op/srand.t			See if srand works
f6ea51
 t/op/sselect.t			See if 4 argument select works
f6ea51
 t/op/stash.t			See if %:: stashes work
f6ea51
 t/op/stat.t			See if stat works
f6ea51
+t/op/stat_errors.t		See if stat and file tests handle threshold errors
f6ea51
 t/op/state.t			See if state variables work
f6ea51
 t/op/study.t			See if study works
f6ea51
 t/op/studytied.t		See if study works with tied scalars
f6ea51
diff --git a/doio.c b/doio.c
f6ea51
index 70d7747..71dc6e4 100644
f6ea51
--- a/doio.c
f6ea51
+++ b/doio.c
f6ea51
@@ -1437,8 +1437,11 @@ Perl_my_stat_flags(pTHX_ const U32 flags)
f6ea51
     if (PL_op->op_flags & OPf_REF) {
f6ea51
 	gv = cGVOP_gv;
f6ea51
       do_fstat:
f6ea51
-        if (gv == PL_defgv)
f6ea51
+        if (gv == PL_defgv) {
f6ea51
+	    if (PL_laststatval < 0)
f6ea51
+		SETERRNO(EBADF,RMS_IFI);
f6ea51
             return PL_laststatval;
f6ea51
+	}
f6ea51
 	io = GvIO(gv);
f6ea51
         do_fstat_have_io:
f6ea51
         PL_laststype = OP_STAT;
f6ea51
@@ -1449,6 +1452,7 @@ Perl_my_stat_flags(pTHX_ const U32 flags)
f6ea51
                 int fd = PerlIO_fileno(IoIFP(io));
f6ea51
                 if (fd < 0) {
f6ea51
                     /* E.g. PerlIO::scalar has no real fd. */
f6ea51
+		    SETERRNO(EBADF,RMS_IFI);
f6ea51
                     return (PL_laststatval = -1);
f6ea51
                 } else {
f6ea51
                     return (PL_laststatval = PerlLIO_fstat(fd, &PL_statcache));
f6ea51
@@ -1459,6 +1463,7 @@ Perl_my_stat_flags(pTHX_ const U32 flags)
f6ea51
         }
f6ea51
 	PL_laststatval = -1;
f6ea51
 	report_evil_fh(gv);
f6ea51
+	SETERRNO(EBADF,RMS_IFI);
f6ea51
 	return -1;
f6ea51
     }
f6ea51
     else if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
f6ea51
@@ -1511,6 +1516,8 @@ Perl_my_lstat_flags(pTHX_ const U32 flags)
f6ea51
 	if (cGVOP_gv == PL_defgv) {
f6ea51
 	    if (PL_laststype != OP_LSTAT)
f6ea51
 		Perl_croak(aTHX_ "%s", no_prev_lstat);
f6ea51
+	    if (PL_laststatval < 0)
f6ea51
+		SETERRNO(EBADF,RMS_IFI);
f6ea51
 	    return PL_laststatval;
f6ea51
 	}
f6ea51
 	PL_laststatval = -1;
f6ea51
@@ -1520,6 +1527,7 @@ Perl_my_lstat_flags(pTHX_ const U32 flags)
f6ea51
 		              "Use of -l on filehandle %" HEKf,
f6ea51
 			      HEKfARG(GvENAME_HEK(cGVOP_gv)));
f6ea51
 	}
f6ea51
+	SETERRNO(EBADF,RMS_IFI);
f6ea51
 	return -1;
f6ea51
     }
f6ea51
     if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
f6ea51
diff --git a/pp_sys.c b/pp_sys.c
f6ea51
index fefbea3..87961f1 100644
f6ea51
--- a/pp_sys.c
f6ea51
+++ b/pp_sys.c
f6ea51
@@ -2925,10 +2925,11 @@ PP(pp_stat)
f6ea51
 		Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
f6ea51
 	}
f6ea51
 
f6ea51
-	if (gv != PL_defgv) {
f6ea51
-	    bool havefp;
f6ea51
+	if (gv == PL_defgv) {
f6ea51
+	    if (PL_laststatval < 0)
f6ea51
+		SETERRNO(EBADF,RMS_IFI);
f6ea51
+	} else {
f6ea51
           do_fstat_have_io:
f6ea51
-	    havefp = FALSE;
f6ea51
 	    PL_laststype = OP_STAT;
f6ea51
 	    PL_statgv = gv ? gv : (GV *)io;
f6ea51
             SvPVCLEAR(PL_statname);
f6ea51
@@ -2939,22 +2940,25 @@ PP(pp_stat)
f6ea51
                     if (IoIFP(io)) {
f6ea51
                         int fd = PerlIO_fileno(IoIFP(io));
f6ea51
                         if (fd < 0) {
f6ea51
+			    report_evil_fh(gv);
f6ea51
                             PL_laststatval = -1;
f6ea51
                             SETERRNO(EBADF,RMS_IFI);
f6ea51
                         } else {
f6ea51
                             PL_laststatval = PerlLIO_fstat(fd, &PL_statcache);
f6ea51
-                            havefp = TRUE;
f6ea51
                         }
f6ea51
                     } else if (IoDIRP(io)) {
f6ea51
                         PL_laststatval =
f6ea51
                             PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
f6ea51
-                        havefp = TRUE;
f6ea51
                     } else {
f6ea51
+			report_evil_fh(gv);
f6ea51
                         PL_laststatval = -1;
f6ea51
+			SETERRNO(EBADF,RMS_IFI);
f6ea51
                     }
f6ea51
-            }
f6ea51
-	    else PL_laststatval = -1;
f6ea51
-	    if (PL_laststatval < 0 && !havefp) report_evil_fh(gv);
f6ea51
+            } else {
f6ea51
+		report_evil_fh(gv);
f6ea51
+		PL_laststatval = -1;
f6ea51
+		SETERRNO(EBADF,RMS_IFI);
f6ea51
+	    }
f6ea51
         }
f6ea51
 
f6ea51
 	if (PL_laststatval < 0) {
f6ea51
@@ -3451,7 +3455,7 @@ PP(pp_fttty)
f6ea51
     else if (name && isDIGIT(*name) && grok_atoUV(name, &uv, NULL) && uv <= PERL_INT_MAX)
f6ea51
         fd = (int)uv;
f6ea51
     else
f6ea51
-	FT_RETURNUNDEF;
f6ea51
+	fd = -1;
f6ea51
     if (fd < 0) {
f6ea51
         SETERRNO(EBADF,RMS_IFI);
f6ea51
 	FT_RETURNUNDEF;
f6ea51
diff --git a/t/op/stat_errors.t b/t/op/stat_errors.t
f6ea51
new file mode 100644
f6ea51
index 0000000..e043c61
f6ea51
--- /dev/null
f6ea51
+++ b/t/op/stat_errors.t
f6ea51
@@ -0,0 +1,57 @@
f6ea51
+#!./perl
f6ea51
+
f6ea51
+BEGIN {
f6ea51
+    chdir 't' if -d 't';
f6ea51
+    require './test.pl';
f6ea51
+    set_up_inc('../lib');
f6ea51
+}
f6ea51
+
f6ea51
+plan(tests => 2*11*29);
f6ea51
+
f6ea51
+use Errno qw(EBADF ENOENT);
f6ea51
+
f6ea51
+open(SCALARFILE, "<", \"wibble") or die $!;
f6ea51
+open(CLOSEDFILE, "<", "./test.pl") or die $!;
f6ea51
+close(CLOSEDFILE) or die $!;
f6ea51
+opendir(CLOSEDDIR, "../lib") or die $!;
f6ea51
+closedir(CLOSEDDIR) or die $!;
f6ea51
+
f6ea51
+foreach my $op (
f6ea51
+    qw(stat lstat),
f6ea51
+    (map { "-$_" } qw(r w x o R W X O e z s f d l p S b c t u g k T B M A C)),
f6ea51
+) {
f6ea51
+    foreach my $arg (
f6ea51
+	(map { ($_, "\\*$_") }
f6ea51
+	    qw(NEVEROPENED SCALARFILE CLOSEDFILE CLOSEDDIR _)),
f6ea51
+	"\"tmpnotexist\"",
f6ea51
+    ) {
f6ea51
+	my $argdesc = $arg;
f6ea51
+	if ($arg eq "_") {
f6ea51
+	    my @z = lstat "tmpnotexist";
f6ea51
+	    $argdesc .= " with prior stat fail";
f6ea51
+	}
f6ea51
+	SKIP: {
f6ea51
+	    if ($op eq "-l" && $arg =~ /\A\\/) {
f6ea51
+		# The op weirdly stringifies the globref and uses it as
f6ea51
+		# a filename, rather than treating it as a file handle.
f6ea51
+		# That might be a bug, but while that behaviour exists it
f6ea51
+		# needs to be exempted from these tests.
f6ea51
+		skip "-l on globref", 2;
f6ea51
+	    }
f6ea51
+	    if ($op eq "-t" && $arg eq "\"tmpnotexist\"") {
f6ea51
+		# The op doesn't operate on filenames.
f6ea51
+		skip "-t on filename", 2;
f6ea51
+	    }
f6ea51
+	    $! = 0;
f6ea51
+	    my $res = eval "$op $arg";
f6ea51
+	    my $err = $!;
f6ea51
+	    is $res, $op =~ /\A-/ ? undef : !!0, "result of $op $arg";
f6ea51
+	    is 0+$err,
f6ea51
+		$arg eq "\"tmpnotexist\"" ||
f6ea51
+		    ($op =~ /\A-[TB]\z/ && $arg =~ /_\z/) ? ENOENT : EBADF,
f6ea51
+		"error from $op $arg";
f6ea51
+	}
f6ea51
+    }
f6ea51
+}
f6ea51
+
f6ea51
+1;
f6ea51
-- 
f6ea51
2.13.6
f6ea51