Skip to content

Commit

Permalink
make $fh->error report errors from both input and output
Browse files Browse the repository at this point in the history
For character devices and sockets perl uses separate PerlIO objects
for input and output so they can be buffered separately.

The IO::Handle::error() method only checked the input stream, so
if a write error occurs error() would still returned false.

Change this so both the input and output streams are checked.

fixes Perl#6799
  • Loading branch information
tonycoz authored and khwilliamson committed Jul 30, 2020
1 parent b4aeee7 commit 89341f8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
12 changes: 8 additions & 4 deletions dist/IO/IO.xs
Expand Up @@ -389,13 +389,17 @@ ungetc(handle, c)

int
ferror(handle)
InputStream handle
SV * handle
PREINIT:
IO *io = sv_2io(handle);
InputStream in = IoIFP(io);
OutputStream out = IoOFP(io);
CODE:
if (handle)
if (in)
#ifdef PerlIO
RETVAL = PerlIO_error(handle);
RETVAL = PerlIO_error(in) || (in != out && PerlIO_error(out));
#else
RETVAL = ferror(handle);
RETVAL = ferror(in) || (in != out && ferror(out));
#endif
else {
RETVAL = -1;
Expand Down
19 changes: 18 additions & 1 deletion dist/IO/t/io_xs.t
Expand Up @@ -11,7 +11,7 @@ BEGIN {
}
}

use Test::More tests => 5;
use Test::More tests => 7;
use IO::File;
use IO::Seekable;

Expand Down Expand Up @@ -50,3 +50,20 @@ SKIP:
ok($fh->sync, "sync to a read only handle")
or diag "sync(): ", $!;
}


SKIP: {
# gh 6799
#
# This isn't really a Linux/BSD specific test, but /dev/full is (I
# hope) reasonably well defined on these. Patches welcome if your platform
# also supports it (or something like it)
skip "no /dev/full or not a /dev/full platform", 2
unless $^O =~ /^(linux|netbsd|freebsd)$/ && -c "/dev/full";
open my $fh, ">", "/dev/full"
or skip "Could not open /dev/full: $!", 2;
$fh->print("a" x 1024);
ok(!$fh->flush, "should fail to flush");
ok($fh->error, "stream should be in error");
close $fh; # silently ignore the error
}

0 comments on commit 89341f8

Please sign in to comment.