Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.91k stars 542 forks source link

$GH=\*STDOUT; open ($GH, '>', \my $tmp); breaks. #7180

Closed p5pRT closed 20 years ago

p5pRT commented 20 years ago

Migrated from rt.perl.org#27714 (status was 'resolved')

Searchable as RT27714$

p5pRT commented 20 years ago

From jcromie@divsol.com

folks\,

perlbug.rep attached. In case it doesnt just inline for you\, heres the jist again​:

GIVEN​:

sub open2scalar {   my $msg = shift;   open ($GH\, '>'\, \my $tmp) or warn $!;   print $GH "hello from open2scalar​: $msg\n";   print "at line# "\, __LINE__\, "​: $tmp"; } open2scalar 'b4 handle init'; $GH = 'foo'; open2scalar 'handle set to string'; $GH = \*STDOUT; open2scalar 'handle set to \STDOUT';

both bleadperl and 5.8.3 yield following​:

[jimc@​harpo bleadperl]$ ./perl -w ~/perl-play/open-bug-test

at line# 44​: hello from open2scalar​: b4 handle init at line# 44​: hello from open2scalar​: handle set to string Bad file descriptor at /home/jimc/perl-play/open-bug-test line 42. hello from open2scalar​: handle set to \STDOUT Use of uninitialized value in concatenation (.) or string at /home/jimc/perl-play/open-bug-test line 44. at line# 44​: [jimc@​harpo bleadperl]$

Note that although the 3rd call also prints\, it is missing the 'at line# 44' part. This is cuz the open failed\, and $GH is still STDOUT. The line#44 prints afterwards\, and after the error about $tmp being uninitialized.

The warning is less than ideal\, but since $! is constrained to match OS errors\, this is probably a DOC-BUG.

I assume this doc-patch belongs in 'perldoc -f open' output\, but I cant find that file ;-)

p5pRT commented 20 years ago

From jcromie@divsol.com

Created by jimc@harpo.jimc.earth

GIVEN​:

sub open2scalar {   my $msg = shift;   open ($GH\, '>'\, \my $tmp) or warn $!;   print $GH "hello from open2scalar​: $msg\n";   print "at line# "\, __LINE__\, "​: $tmp"; } open2scalar 'b4 handle init'; $GH = 'foo'; open2scalar 'handle set to string'; $GH = \*STDOUT; open2scalar 'handle set to \STDOUT';

both bleadperl and 5.8.3 yield following​:

[jimc@​harpo bleadperl]$ ./perl -w ~/perl-play/open-bug-test

at line# 44​: hello from open2scalar​: b4 handle init at line# 44​: hello from open2scalar​: handle set to string Bad file descriptor at /home/jimc/perl-play/open-bug-test line 42. hello from open2scalar​: handle set to \STDOUT Use of uninitialized value in concatenation (.) or string at /home/jimc/perl-play/open-bug-test line 44. at line# 44​: [jimc@​harpo bleadperl]$

Note that although the 3rd call also prints\, it is missing the 'at line# 44' part. This is cuz the open failed\, and $GH is still STDOUT. The line#44 prints afterwards\, and after the error about $tmp being uninitialized.

The warning is less than ideal\, but since $! is constrained to match OS errors\, this is probably a DOC-BUG.

I assume this doc-patch belongs in 'perldoc -f open' output\, but I cant find that file ;-)

Perl Info ``` Flags: category=core severity=low Site configuration information for perl v5.8.3: Configured by jimc at Wed Jan 14 15:30:49 MST 2004. Summary of my perl5 (revision 5.0 version 8 subversion 3) configuration: Platform: osname=linux, osvers=2.4.20-28.7, archname=i686-linux-thread-multi uname='linux harpo.jimc.earth 2.4.20-28.7 #1 thu dec 18 11:31:59 est 2003 i686 unknown ' config_args='-DDEBUGGING -Dusethreads -Dd_dosuid -Dd_semctl_semun -Di_db -Di_ndbm -Di_gdbm -Di_shadow -Di_syslog' hint=recommended, useposix=true, d_sigaction=define usethreads=define use5005threads=undef useithreads=define usemultiplicity=define useperlio=define d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm', optimize='-O2', cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-strict-aliasing -I/usr/local/include -I/usr/include/gdbm' ccversion='', gccversion='2.96 20000731 (Red Hat Linux 7.2 2.96-112.7.2)', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8 alignbytes=4, prototype=define Linker and Libraries: ld='cc', ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib libs=-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lpthread -lc perllibs=-lnsl -ldl -lm -lcrypt -lutil -lpthread -lc libc=/lib/libc-2.2.4.so, so=so, useshrplib=false, libperl=libperl.a gnulibc_version='2.2.4' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic' cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib' Locally applied patches: @INC for perl v5.8.3: /usr/local/lib/perl5/5.8.3/i686-linux-thread-multi /usr/local/lib/perl5/5.8.3 /usr/local/lib/perl5/site_perl/5.8.3/i686-linux-thread-multi /usr/local/lib/perl5/site_perl/5.8.3 /usr/local/lib/perl5/site_perl/5.8.2/i686-linux-thread-multi /usr/local/lib/perl5/site_perl/5.8.2 /usr/local/lib/perl5/site_perl/5.8.1/i686-linux-thread-multi /usr/local/lib/perl5/site_perl/5.8.1 /usr/local/lib/perl5/site_perl/5.8.0 /usr/local/lib/perl5/site_perl/5.6.2 /usr/local/lib/perl5/site_perl . Environment for perl v5.8.3: HOME=/home/jimc LANG=en_US LANGUAGE (unset) LD_LIBRARY_PATH (unset) LOGDIR (unset) PATH=/usr/local/apache/bin:/usr/local/bin:/usr/local/apache/bin:/usr/local/bin:/bin:/usr/bin:/usr/bin/X11:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/usr/java/j2sdk1.4.1_02/bin:/usr/local/mozilla:/home/jimc/bin:./bin:.:/usr/sbin:/sbin PERL_BADLANG (unset) SHELL=/bin/bash ```
p5pRT commented 20 years ago

From stas@stason.org

Jim Cromie (via RT) wrote​:

# New Ticket Created by Jim Cromie # Please include the string​: [perl #27714] # in the subject line of all future correspondence about this issue. # \<URL​: http​://rt.perl.org​:80/rt3/Ticket/Display.html?id=27714 >

folks\,

perlbug.rep attached. In case it doesnt just inline for you\, heres the jist again​:

GIVEN​:

sub open2scalar { my $msg = shift; open ($GH\, '>'\, \my $tmp) or warn $!; print $GH "hello from open2scalar​: $msg\n"; print "at line# "\, __LINE__\, "​: $tmp"; } open2scalar 'b4 handle init'; $GH = 'foo'; open2scalar 'handle set to string'; $GH = \*STDOUT; open2scalar 'handle set to \STDOUT';

both bleadperl and 5.8.3 yield following​:

[jimc@​harpo bleadperl]$ ./perl -w ~/perl-play/open-bug-test

at line# 44​: hello from open2scalar​: b4 handle init at line# 44​: hello from open2scalar​: handle set to string Bad file descriptor at /home/jimc/perl-play/open-bug-test line 42. hello from open2scalar​: handle set to \STDOUT Use of uninitialized value in concatenation (.) or string at /home/jimc/perl-play/open-bug-test line 44. at line# 44​: [jimc@​harpo bleadperl]$

Note that although the 3rd call also prints\, it is missing the 'at line# 44' part. This is cuz the open failed\, and $GH is still STDOUT. The line#44 prints afterwards\, and after the error about $tmp being uninitialized.

The warning is less than ideal\, but since $! is constrained to match OS errors\, this is probably a DOC-BUG.

I assume this doc-patch belongs in 'perldoc -f open' output\, but I cant find that file ;-)

Jim\,

perldoc -f open​:

  File handles can be opened to "in memory" files held in Perl   scalars via​:

  open($fh\, '>'\, \$variable) || ..

  Though if you try to re-open "STDOUT" or "STDERR" as an "in   memory" file\, you have to close it first​:

  close STDOUT;   open STDOUT\, '>'\, \$variable or die "Can't open STDOUT​: $!";

so your code needs to be​:

sub open2scalar {   my $msg = shift;   open ($GH\, '>'\, \my $tmp) or warn $!;   print $GH "hello from open2scalar​: $msg\n";   print "at line# "\, __LINE__\, "​: $tmp"; } open2scalar 'b4 handle init'; $GH = 'foo'; open2scalar 'handle set to string'; close STDOUT; $GH = \*STDOUT; open2scalar 'handle set to \STDOUT';

or you could arrange for open2scalar to close the fh before it opens it.

__________________________________________________________________ Stas Bekman JAm_pH ------> Just Another mod_perl Hacker http​://stason.org/ mod_perl Guide ---> http​://perl.apache.org mailto​:stas@​stason.org http​://use.perl.org http​://apacheweek.com http​://modperlbook.org http​://apache.org http​://ticketmaster.com

p5pRT commented 20 years ago

The RT System itself - Status changed from 'new' to 'open'

p5pRT commented 20 years ago

From @chipdude

"Well\, don't do that\, then."

What with the select() operator\, there's no need to reopen STDOUT in magic fashion. (There can be a need to reopen STDOUT in non-magic ways\, for inheritance by child processes. But magic doesn't survive exec()\, so there's no point with that.) -- Chip Salzenberg - a.k.a. - \chip@&#8203;pobox\.com "I wanted to play hopscotch with the impenetrable mystery of existence\,   but he stepped in a wormhole and had to go in early." // MST3K

p5pRT commented 20 years ago

From jcromie@divsol.com

Stas Bekman wrote​:

perldoc -f open​:

thanks Stas - that directly addresses my prob - ie 'dont do that'. I obviously missed that somehow.

NOTABUG.

as to your suggested workaround\, I solved it differently\,

open (my $tmp\, '>'\, \my $buff); $GH = $tmp;

This should implicitly close whatever's already in $GH\, without inadvertently trying to close an undef\, or having to test its current value.

I dont know whether this has any other corner cases\, but I raise it here cuz I extracted this test-case from my patch #24821\, and ICBW there also. (youre welcome to comment there too ;-)

p5pRT commented 20 years ago

From stas@stason.org

jim cromie wrote​:

Stas Bekman wrote​:

perldoc -f open​:

thanks Stas - that directly addresses my prob - ie 'dont do that'. I obviously missed that somehow.

Besides\, in-memory vars are broken with regards to STD streams. I've reported that earlier\, but didn't submit a proper bug report. I will do that now.

__________________________________________________________________ Stas Bekman JAm_pH ------> Just Another mod_perl Hacker http​://stason.org/ mod_perl Guide ---> http​://perl.apache.org mailto​:stas@​stason.org http​://use.perl.org http​://apacheweek.com http​://modperlbook.org http​://apache.org http​://ticketmaster.com

p5pRT commented 20 years ago

@rspier - Status changed from 'open' to 'resolved'