8446b7
From 8a2562bec7cd9f8eff6812f340f99dddd028bb33 Mon Sep 17 00:00:00 2001
8446b7
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
8446b7
Date: Thu, 6 Aug 2020 10:51:56 +0200
8446b7
Subject: [PATCH] IO::Handle: Fix a spurious error reported for regular file
8446b7
 handles
8446b7
MIME-Version: 1.0
8446b7
Content-Type: text/plain; charset=UTF-8
8446b7
Content-Transfer-Encoding: 8bit
8446b7
8446b7
89341f87 fix for GH #6799 introduced a regression when calling error()
8446b7
on an IO::Handle object that was opened for reading a regular file:
8446b7
8446b7
$ perl -e 'open my $f, q{<}, q{/etc/hosts} or die; print qq{error\n} if $f->error'
8446b7
error
8446b7
8446b7
In case of a regular file opened for reading, IoOFP() returns NULL and
8446b7
PerlIO_error(NULL) reports -1. Compare to the case of a file opened
8446b7
for writing when both IoIFP() and IoOFP() return non-NULL, equaled
8446b7
pointer.
8446b7
8446b7
This patch fixes handling the case of the NULL output stream.
8446b7
8446b7
GH #18019
8446b7
8446b7
Signed-off-by: Petr Písař <ppisar@redhat.com>
8446b7
---
8446b7
 dist/IO/IO.xs     |  4 ++--
8446b7
 dist/IO/t/io_xs.t | 10 +++++++++-
8446b7
 2 files changed, 11 insertions(+), 3 deletions(-)
8446b7
8446b7
diff --git a/dist/IO/IO.xs b/dist/IO/IO.xs
8446b7
index 9158106416..fb009774c4 100644
8446b7
--- a/dist/IO/IO.xs
8446b7
+++ b/dist/IO/IO.xs
8446b7
@@ -397,9 +397,9 @@ ferror(handle)
8446b7
     CODE:
8446b7
 	if (in)
8446b7
 #ifdef PerlIO
8446b7
-	    RETVAL = PerlIO_error(in) || (in != out && PerlIO_error(out));
8446b7
+	    RETVAL = PerlIO_error(in) || (out && in != out && PerlIO_error(out));
8446b7
 #else
8446b7
-	    RETVAL = ferror(in) || (in != out && ferror(out));
8446b7
+	    RETVAL = ferror(in) || (out && in != out && ferror(out));
8446b7
 #endif
8446b7
 	else {
8446b7
 	    RETVAL = -1;
8446b7
diff --git a/dist/IO/t/io_xs.t b/dist/IO/t/io_xs.t
8446b7
index a8833b0651..4657088629 100644
8446b7
--- a/dist/IO/t/io_xs.t
8446b7
+++ b/dist/IO/t/io_xs.t
8446b7
@@ -11,7 +11,7 @@ BEGIN {
8446b7
     }
8446b7
 }
8446b7
 
8446b7
-use Test::More tests => 8;
8446b7
+use Test::More tests => 10;
8446b7
 use IO::File;
8446b7
 use IO::Seekable;
8446b7
 
8446b7
@@ -69,3 +69,11 @@ SKIP: {
8446b7
     ok(!$fh->error, "check clearerr removed the error");
8446b7
     close $fh; # silently ignore the error
8446b7
 }
8446b7
+
8446b7
+{
8446b7
+    # [GH #18019] IO::Handle->error misreported an error after successully
8446b7
+    # opening a regular file for reading. It was a regression in GH #6799 fix.
8446b7
+    ok(open(my $fh, '<', __FILE__), "a regular file opened for reading");
8446b7
+    ok(!$fh->error, "no spurious error reported by error()");
8446b7
+    close $fh;
8446b7
+}
8446b7
-- 
8446b7
2.25.4
8446b7