File::stat has a serious bug in Perl 5.6.1

Just thought I'd write a note about this, so that Google might warn others about it.

There is a bug in the Perl standard module File::stat. It is present at least in the version that comes with Perl 5.6.1, and has been fixed in the version that comes with Perl 5.8.1.

The trouble is, it doesn't detect properly errors in stating the file (in the stat sub it provides to override the built-in one). It assumes that if the call to the built-in stat fails, then you must have passed it a file handle rather than a filename.

Here is a sample program which demonstrates the problem:

#!/usr/bin/perl -w
use strict;
use File::stat;

my $s = stat('/this/does/not/exist')
    or die "Error stating file: $!\n";

print "Size: ", $s->size, "\n";

Symptoms

If warnings are turned on, as above, then the attempt to use the filename as a file handle (after using it as a filename has failed) will make Perl print a warning like this:

stat() on unopened filehandle /this/does/not/exist
at /usr/share/perl/5.6.1/File/stat.pm line 49.
 

Whether or not warnings are turned on, the stat sub will return false to indicate an error, but $! will already have been emptied by the second call to the built-in stat.

Solution

Don't use File::stat unless you know you won't be using Perl 5.6.1. Go back to the old-fashioned stat function, ugly though it is.

< Interesting revision control interface idea | Glühwein and Rauchzipferl >

Miniblog

(nuggets of inanity)

Tuesday Apr 24th 2007, 16:54 »
Just took the annual web design survey that AListApart do. I don't realy consider myself to be a web designer, but I have been doing a lot of HTML and CSS lately.
Monday Apr 23rd 2007, 18:23 »
Strange, there appears to be a bare-knuckle boxing match going on in the field outside my flat. Wish they wouldn't make so much noise about it.
Thursday Mar 1st 2007, 18:47 »
“In its written form, Hebrew has no vowels, making it the ideal language for texting.”
—Said in jest on some Radio 4 programme just now.

Archive: 2007 · 2006 · 2005 · 2004
Feed