Skip to content

Commit 8a2562b

Browse files
ppisarkhwilliamson
authored andcommitted
IO::Handle: Fix a spurious error reported for regular file handles
89341f8 fix for GH #6799 introduced a regression when calling error() on an IO::Handle object that was opened for reading a regular file: $ perl -e 'open my $f, q{<}, q{/etc/hosts} or die; print qq{error\n} if $f->error' error In case of a regular file opened for reading, IoOFP() returns NULL and PerlIO_error(NULL) reports -1. Compare to the case of a file opened for writing when both IoIFP() and IoOFP() return non-NULL, equaled pointer. This patch fixes handling the case of the NULL output stream. GH #18019
1 parent 1fb364e commit 8a2562b

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

dist/IO/IO.xs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,9 @@ ferror(handle)
397397
CODE:
398398
if (in)
399399
#ifdef PerlIO
400-
RETVAL = PerlIO_error(in) || (in != out && PerlIO_error(out));
400+
RETVAL = PerlIO_error(in) || (out && in != out && PerlIO_error(out));
401401
#else
402-
RETVAL = ferror(in) || (in != out && ferror(out));
402+
RETVAL = ferror(in) || (out && in != out && ferror(out));
403403
#endif
404404
else {
405405
RETVAL = -1;

dist/IO/t/io_xs.t

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ BEGIN {
1111
}
1212
}
1313

14-
use Test::More tests => 8;
14+
use Test::More tests => 10;
1515
use IO::File;
1616
use IO::Seekable;
1717

@@ -69,3 +69,11 @@ SKIP: {
6969
ok(!$fh->error, "check clearerr removed the error");
7070
close $fh; # silently ignore the error
7171
}
72+
73+
{
74+
# [GH #18019] IO::Handle->error misreported an error after successully
75+
# opening a regular file for reading. It was a regression in GH #6799 fix.
76+
ok(open(my $fh, '<', __FILE__), "a regular file opened for reading");
77+
ok(!$fh->error, "no spurious error reported by error()");
78+
close $fh;
79+
}

0 commit comments

Comments
 (0)