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