diff options
author | Yves Orton <demerphq@gmail.com> | 2022-03-03 15:24:03 +0100 |
---|---|---|
committer | Yves Orton <demerphq@gmail.com> | 2022-03-07 16:22:12 +0100 |
commit | 8b03aeb95ab72abdb2fa40f2d1196ce42f34708d (patch) | |
tree | 5ec32d32dc62e922697511b5404d9440d7ea006e /t | |
parent | 7ea8b04b5a0e6952b7ffd5a8fd96468b72da6bea (diff) | |
download | perl-8b03aeb95ab72abdb2fa40f2d1196ce42f34708d.tar.gz |
Fix GH Issue #19472: read warnings from open($fh,">",\(my $x))
We produce all kinds of warnings if someone opens a scalar reference
that is undef. Prior to this we handled write operations ok, at
least in blead, but read operations would produce a plethora of
warnings. To me this analogous to treating an undef var as hash
and trying to read from it, we autovivify the undef var to be
a hash. So in this case we should just "autovivify" the referenced
scalar to be an empty string.
Eg. before this patch:
./perl -Ilib -wle'open my $fh,"+>", \(my $v); my @x=<$fh>; print 0+@x'
Use of uninitialized value $fh in <HANDLE> at -e line 1.
Use of uninitialized value $fh in <HANDLE> at -e line 1.
Use of uninitialized value $fh in <HANDLE> at -e line 1.
Use of uninitialized value $fh in <HANDLE> at -e line 1.
Use of uninitialized value $fh in <HANDLE> at -e line 1.
Use of uninitialized value $fh in <HANDLE> at -e line 1.
Use of uninitialized value $fh in <HANDLE> at -e line 1.
0
After it:
./perl -Ilib -wle'open my $fh,"+>", \(my $v); my @x=<$fh>; print 0+@x'
0
Diffstat (limited to 't')
-rw-r--r-- | t/io/open.t | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/t/io/open.t b/t/io/open.t index 7fa492342d..86ca8a8498 100644 --- a/t/io/open.t +++ b/t/io/open.t @@ -10,7 +10,7 @@ $| = 1; use warnings; use Config; -plan tests => 188; +plan tests => 193; sub ok_cloexec { SKIP: { @@ -410,6 +410,28 @@ SKIP: { or unlink \*STDOUT; } +# [GH Issue #19472] Opening a reference to an undef scalar +SKIP: { + skip_if_miniperl("no dynamic loading on miniperl, so can't load PerlIO::scalar", 5); + use strict; + my $var; + open my $fh, "+>", \$var; + is defined($var) ? "defined" : "undef", "defined", + '[GH Issue #19472]: open $fh, ">", \$undef_var leaves var defined'; + is $var, "", + '[GH Issue #19472]: open $fh, ">", \$undef_var leaves var empty string'; + my $warn_count= 0; + my $warn_text= ""; + local $SIG{__WARN__}= sub { $warn_count++; $warn_text .= shift; }; + my @x= <$fh>; + is $warn_count, 0, '[GH Issue #19472]: read after open $fh, ">", ' . + '\$undef_var produces 0 warnings'; + is $warn_text, "", '[GH Issue #19472]: read after open $fh, ">", ' . + '\$undef_var produces no warning text'; + is 0+@x, 0, '[GH Issue #19472]: <$fh> after open $fh, ">", ' . + '\$undef_var returns nothing'; +} + # check that we can call methods on filehandles auto-magically # and have IO::File loaded for us SKIP: { |