develooper Front page | perl.perl5.porters | Postings from August 2010

[perl #77388] Stacked file tests with -T, -t and/or -B remove too much from the stack

From:
Bram
Date:
August 23, 2010 15:02
Subject:
[perl #77388] Stacked file tests with -T, -t and/or -B remove too much from the stack
Message ID:
rt-3.6.HEAD-5116-1282589511-314.77388-75-0@perl.org
# New Ticket Created by  Bram 
# Please include the string:  [perl #77388]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=77388 >


While writing tests for another bug I discovered a bug in the stacked  
file tests.
When -T, -t or -B are used in a stacked file tests then too much is  
removed from the stack.

Example:

#!/usr/bin/perl -l
use strict;
use warnings;
use Test::More tests => 2;

my $count;
my $t;
{
     $count = 0;
     for my $m ("c", "d") {
         if ($count == 0) {
             $t = -t -e $^X ? 0 : "bar";
         }
         elsif ($count == 1) {
             is($m, "d", "-t -e \$^X did not remove too many values  
from the stack");
         }
         $count++;
     }

     $count = 0;
     for my $m ("e", "f") {
         if ($count == 0) {
             $t = -T -e $^X ? 0 : "baz";
         }
         elsif ($count == 1) {
             is($m, "f", "-T -e \$^X did not remove too many values  
from the stack");
         }
         $count++;
     }
}
__END__
The output of this on blead (81b00ac76d16334c61edb1e9a4a364d00d0db5c6)  
and perl-5.10.0:

1..2
not ok 1 - -t -e $^X did not remove too many values from the stack
#   Failed test '-t -e $^X did not remove too many values from the stack'
#   at /tmp/filetest.pl line 12.
#          got: 'bar'
#     expected: 'd'
not ok 2 - -T -e $^X did not remove too many values from the stack
#   Failed test '-T -e $^X did not remove too many values from the stack'
#   at /tmp/filetest.pl line 23.
#          got: 'baz'
#     expected: 'f'
# Looks like you failed 2 tests of 2.


A test case that is fatal:

#!/usr/bin/perl -l

use strict;
use warnings;
use Test::More tests => 1;

my $count;
my $t;
{
     $count = 0;
     for my $m ("g", "h") {
         if ($count == 0) {
             my $foo;
             $foo->{bar} = -T -e $^X ? 0 : "baz";
         }
         elsif ($count == 1) {
             is($m, "f", "-T -e \$^X did not remove too many values  
from the stack");
         }
         $count++;
     }
}
__END__

Output:
1..1
Use of freed value in iteration at /tmp/filetest.pl line 19.
# Looks like your test exited with 2 before it could output anything.



I had a talk with Jan Dubois about this (because he fixed a similar  
bug with -u/-g/-k on windows) and this are his notes:
  <jdb> It happens because pp_fttty and pp_fttext don't know about  
stacked file tests
  <jdb> The problem is that the stacked filetest doesn't leave the  
filename on the stack, it leaves the result of the test only.
  <jdb> So the next test in the sequence will pop the result with  
STACKED_FTEST_CHECK and either return early with a FALSE value, or  
continue checking
  <jdb> The checking will have to rely on PL_laststatval for the next  
test in the sequence
  <jdb> But the stat buffer doesn't contain the information that -T needs
  <jdb> So it always pops something from the stack, even though there  
is nothing there in this case
  <Bram> I see. Is it fixable? Or should I create a bug report for it?
  <jdb> For pp_ftis is this hard to see because the actual stack logic  
is hidden in my_stat() in doio.c (my_stat_flags in 5.12)
  <jdb> Please create a bug report, I'm not sure how you can fix it easily.
  <jdb> Because the filename is gone by the time the second stacked  
filetest is executed.  So how are you going to make the check now?
  <jdb> And extending my_stat() is not an option because -T is *expensive*
  <jdb> It actually opens the file and reads the first 1024 bytes or  
something like that.
  <Bram> I'm not sure... the fix could be to disallow -T and -t in  
stacked file tests but I don't know how complicated that is..



Best regards,

Bram






nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About