On Sat, Jul 11, 2015 at 07:41:40PM -0400, Brandon Allbery wrote:
> On Sat, Jul 11, 2015 at 7:28 PM, Alex Jakimenko <
> perl6-bugs-follo...@perl.org> wrote:
> 
> > $ perl6 somefolder
> > Error while reading from file: Reading from filehandle failed: illegal
> > operation on a directory
> >
> 
> Note that behavior here can differ substantially: some OSes won't let you
> open a directory (Windows, I think); some let you open but not read (Linux?
> must use getdents(2)), some let you read (*BSD / OS X, and you get "raw"
> directory entries).

Perl 5 has an explicit test for "is it a directory", to get a consistent error
message, and (implied by the commit message) get silent success on some
platforms:

commit 1dfef69b3a0643e5cf8879e1476b0163c3e8a9b2
Author: Ricardo Signes <r...@cpan.org>
Date:   Sat Jan 5 20:30:48 2013 -0500

    croak on an attempt to run a directory as a script
    
    How many times have I meant to run "perl -I lib myprog" but instead
    run "perl lib myprog" only to exit 0 because that's what perl does
    when you try to run a directory as a script (at least on unix)?  Many.
    
    perl should croak when instructed to execute a directory.
    [perl #61362] suggests it already does so on Win32.  Now it does it
    everywhere.  Tests not yet written.

diff --git a/perl.c b/perl.c
index c7e1d54..d4f13df 100644
--- a/perl.c
+++ b/perl.c
@@ -3649,6 +3649,7 @@ S_open_script(pTHX_ const char *scriptname, bool 
dosearch, bool *suidscript)
     int fdscript = -1;
     PerlIO *rsfp = NULL;
     dVAR;
+    Stat_t tmpstatbuf;
 
     PERL_ARGS_ASSERT_OPEN_SCRIPT;
 
@@ -3758,6 +3759,13 @@ S_open_script(pTHX_ const char *scriptname, bool 
dosearch, bool *suidscript)
     /* ensure close-on-exec */
     fcntl(PerlIO_fileno(rsfp), F_SETFD, 1);
 #endif
+
+    if (PerlLIO_fstat(PerlIO_fileno(rsfp), &tmpstatbuf) >= 0
+        && S_ISDIR(tmpstatbuf.st_mode))
+        Perl_croak(aTHX_ "Can't open perl script \"%s\": %s\n",
+            CopFILE(PL_curcop),
+            strerror(EISDIR));
+
     return rsfp;
 }
 

I think that Rakudo should make the same check.
(And I think that "not a directory" is better than some attempt to whitelist
"file", er wait, "file or symlink", er wait "file, symlink or named pipe" etc)

Nicholas Clark

Reply via email to