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