develooper Front page | perl.perl5.porters | Postings from August 2008

[perl #43097] [PATCH] print doesn't overwrite $!

Thread Next
From:
Christoph Bussenius via RT
Date:
August 19, 2008 00:04
Subject:
[perl #43097] [PATCH] print doesn't overwrite $!
Message ID:
rt-3.6.HEAD-29759-1219077613-483.43097-15-0@perl.org
> open STDOUT, '>/dev/full' or die $!;
> $|++;
> for (0..2) {
>     print 'x' or warn $!;
>     open my $f, '</no/path/to/file';
> }
> 
> This script should print "No space left on device" three times
> (because this is what you get for writing to /dev/full.) However, what
> it does print is this:
> 
> No space left on device at bug.pl line 4.
> No such file or directory at bug.pl line 4.
> No such file or directory at bug.pl line 4.

This bug is still in perl 5.8.8 and 5.10.0 and blead.

I think I have fixed this bug.  After giving this careful thought, I
think that the patch below is the proper way to fix this; but I would
appreciate feedback from someone with more experience.

The problem is:

pp_print calls do_print.  If that is successful *and* the file-handle is
autoflushing, pp_print calls PerlIO_flush.
The success of do_print depends on the handle's error flag, which gets
set when a flush fails and will never be cleared again when writing into
the buffer; that means the filehandle will never be flushed again (until
the buffer fills up).
My fix simply clears the error flag before each write.

This has another implication:  When you get an error from print and then
print an empty string, the second print will be successful.  This hasn't
been the case previously, but I think it's the right behaviour.

I've also added tests for both the $! thing and the empty-string thing.

The tests don't use /dev/full as in the original report because I don't
think that would be portable.  Instead they produce a print error by
printing to a closed pipe.

Regards,
Christoph


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About