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