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

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

Thread Next
Christoph Bussenius via RT
August 19, 2008 00:04
[perl #43097] [PATCH] print doesn't overwrite $!
Message ID:
> 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 line 4.
> No such file or directory at line 4.
> No such file or directory at 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.


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About