summaryrefslogtreecommitdiff
path: root/ext/POSIX
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2011-09-06 14:12:22 +0200
committerNicholas Clark <nick@ccl4.org>2011-09-13 11:28:04 +0200
commit4e453fcc36412d4d4c1f6ea1c3558786137b81db (patch)
tree9eab2be64893bbff415374feb3d794c2c675c130 /ext/POSIX
parent674d0cd93bd328db0075c660030401d94c10c1e0 (diff)
downloadperl-4e453fcc36412d4d4c1f6ea1c3558786137b81db.tar.gz
In termios.t, avoid reading uninitialised memory in the tests.
If no terminal devices are found, or getattr() fails on all of them, then the struct termios wrapped by the POSIX:Termios object will consist of initialised memory. In this case, it's not possible to use getcc() or the other get*() methods on it. Try harder to find a terminal device - as well as STDIN, STDOUT and STDERR also try to open the controlling terminal directly.
Diffstat (limited to 'ext/POSIX')
-rw-r--r--ext/POSIX/t/termios.t61
1 files changed, 39 insertions, 22 deletions
diff --git a/ext/POSIX/t/termios.t b/ext/POSIX/t/termios.t
index 9e00127d05..e68fb5ad35 100644
--- a/ext/POSIX/t/termios.t
+++ b/ext/POSIX/t/termios.t
@@ -15,34 +15,51 @@ plan skip_all => $@
if !eval "POSIX::Termios->new; 1" && $@ =~ /termios not implemented/;
-# create a new object
-my $termios = eval { POSIX::Termios->new };
-is( $@, '', "calling POSIX::Termios->new" );
-isa_ok( $termios, "POSIX::Termios", "\tchecking the type of the object" );
-
-# testing getattr()
-foreach my $name (qw(STDIN STDOUT STDERR)) {
- my $handle = $::{$name};
- SKIP: {
- skip("$name not a tty", 2) unless -t $handle;
+# A termios struct that we've successfully read from a terminal device:
+my $termios;
+
+foreach (undef, qw(STDIN STDOUT STDERR)) {
+ SKIP:
+ {
+ my ($name, $handle);
+ if (defined $_) {
+ $name = $_;
+ $handle = $::{$name};
+ } else {
+ $name = POSIX::ctermid();
+ skip("Can't get name of controlling terminal", 4)
+ unless defined $name;
+ open $handle, '<', $name or skip("can't open $name: $!", 4);
+ }
+
+ skip("$name not a tty", 4) unless -t $handle;
+
+ my $t = eval { POSIX::Termios->new };
+ is($@, '', "calling POSIX::Termios->new");
+ isa_ok($t, "POSIX::Termios", "checking the type of the object");
+
my $fileno = fileno $handle;
- my $r = eval { $termios->getattr($fileno) };
+ my $r = eval { $t->getattr($fileno) };
is($@, '', "calling getattr($fileno) for $name");
- isnt($r, undef, "returned value ($r) is defined");
+ if(isnt($r, undef, "returned value ($r) is defined")) {
+ $termios = $t;
+ }
}
}
-# testing getcc()
-for my $i (0..NCCS-1) {
- my $r = eval { $termios->getcc($i) };
- is( $@, '', "calling getcc($i)" );
- like($r, qr/\A-?[0-9]+\z/, 'returns an integer');
-}
+if (defined $termios) {
+ # testing getcc()
+ for my $i (0 .. NCCS-1) {
+ my $r = eval { $termios->getcc($i) };
+ is($@, '', "calling getcc($i)");
+ like($r, qr/\A-?[0-9]+\z/, 'returns an integer');
+ }
-for my $method (qw(getcflag getiflag getispeed getlflag getoflag getospeed)) {
- my $r = eval { $termios->$method() };
- is( $@, '', "calling $method()" );
- like($r, qr/\A-?[0-9]+\z/, 'returns an integer');
+ for my $method (qw(getcflag getiflag getispeed getlflag getoflag getospeed)) {
+ my $r = eval { $termios->$method() };
+ is($@, '', "calling $method()");
+ like($r, qr/\A-?[0-9]+\z/, 'returns an integer');
+ }
}
done_testing();