Make -T HANDLE and -B HANDLE always set last stat type
authorFather Chrysostomos <sprout@cpan.org>
Sat, 14 Jan 2012 07:50:15 +0000 (23:50 -0800)
committerFather Chrysostomos <sprout@cpan.org>
Sat, 14 Jan 2012 07:50:15 +0000 (23:50 -0800)
-T and -B on handles always set PL_laststatval (which indicates the
success of the previous stat).  But they don’t set the last stat type
(PL_laststype) for closed filehandles.  Those two should always go
together.  stat and -r, -w etc., always set PL_laststype for a closed
or missing filehandle.

pp_sys.c
t/op/filetest.t

index 41cf64d..3757401 100644 (file)
--- a/pp_sys.c
+++ b/pp_sys.c
@@ -3332,11 +3332,11 @@ PP(pp_fttext)
            io = GvIO(PL_statgv);
        }
        PL_laststatval = -1;
+       PL_laststype = OP_STAT;
        if (io && IoIFP(io)) {
            if (! PerlIO_has_base(IoIFP(io)))
                DIE(aTHX_ "-T and -B not implemented on filehandles");
            PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
-           PL_laststype = OP_STAT;
            if (PL_laststatval < 0)
                RETPUSHUNDEF;
            if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
index 65d9147..26d3cdd 100644 (file)
@@ -10,7 +10,7 @@ BEGIN {
 }
 
 use Config;
-plan(tests => 43 + 27*14);
+plan(tests => 44 + 27*14);
 
 ok( -d 'op' );
 ok( -f 'TEST' );
@@ -253,7 +253,7 @@ for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
 my $Perl = which_perl();
 
 SKIP: {
-    skip "no -T on filehandles", 6 unless eval { -T STDERR; 1 };
+    skip "no -T on filehandles", 7 unless eval { -T STDERR; 1 };
 
     # Test that -T HANDLE sets the last stat type
     -l "perl.c";   # last stat type is now lstat
@@ -293,6 +293,12 @@ SKIP: {
     close $fh;
     -T _;
     ok !stat _, '-T _ on closed filehandle resets stat info';
+
+    lstat "test.pl";
+    -T $fh; # closed
+    eval { lstat _ };
+    like $@, qr/^The stat preceding lstat\(\) wasn't an lstat at /,
+       '-T on closed handle resets last stat type';
 }
 
 is runperl(prog => '-T _', switches => ['-w'], stderr => 1), "",