Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LTA error message when executing directories as perl6 code #4396

Closed
p6rt opened this issue Jul 11, 2015 · 6 comments
Closed

LTA error message when executing directories as perl6 code #4396

p6rt opened this issue Jul 11, 2015 · 6 comments

Comments

@p6rt
Copy link

p6rt commented Jul 11, 2015

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

Searchable as RT125600$

@p6rt
Copy link
Author

p6rt commented Jul 11, 2015

From @AlexDaniel

$ mkdir somefolder
$ perl6 somefolder
Error while reading from file​: Reading from filehandle failed​: illegal
operation on a directory
  at gen/moar/stage2/NQPHLL.nqp​:1483
(/home/alex/git/rakudobrew/moar-nom/install/share/nqp/lib/NQPHLL.moarvm​::71)
from gen/moar/stage2/NQPHLL.nqp​:1459
(/home/alex/git/rakudobrew/moar-nom/install/share/nqp/lib/NQPHLL.moarvm​:evalfiles​:64)
from gen/moar/stage2/NQPHLL.nqp​:1386
(/home/alex/git/rakudobrew/moar-nom/install/share/nqp/lib/NQPHLL.moarvm​:command_eval​:214)
from src/Perl6/Compiler.nqp​:59
(/home/alex/git/rakudobrew/moar-nom/install/share/nqp/lib/Perl6/Compiler.moarvm​:command_eval​:93)
from gen/moar/stage2/NQPHLL.nqp​:1360
(/home/alex/git/rakudobrew/moar-nom/install/share/nqp/lib/NQPHLL.moarvm​:command_line​:116)
from src/gen/m-main.nqp​:39
(/home/alex/git/rakudobrew/moar-nom/install/share/perl6/runtime/perl6.moarvm​:MAIN​:18)
from src/gen/m-main.nqp​:35
(/home/alex/git/rakudobrew/moar-nom/install/share/perl6/runtime/perl6.moarvm​:<mainline>​:197)
from <unknown>​:1
(/home/alex/git/rakudobrew/moar-nom/install/share/perl6/runtime/perl6.moarvm​:<main>​:8)
from <unknown>​:1
(/home/alex/git/rakudobrew/moar-nom/install/share/perl6/runtime/perl6.moarvm​:<entry>​:9)

Although it mentions that something is a directory, still the error could
be better.

@p6rt
Copy link
Author

p6rt commented Jul 11, 2015

From @geekosaur

On Sat, Jul 11, 2015 at 7​:28 PM, Alex Jakimenko <
perl6-bugs-followup@​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).

--
brandon s allbery kf8nh sine nomine associates
allbery.b@​gmail.com ballbery@​sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

@p6rt
Copy link
Author

p6rt commented Jul 11, 2015

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

@p6rt
Copy link
Author

p6rt commented Jul 12, 2015

From @nwc10

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-followup@​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 <rjbs@​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.

Inline Patch
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

@p6rt
Copy link
Author

p6rt commented Jul 15, 2015

From @jnthn

On Sun Jul 12 02​:19​:35 2015, nicholas wrote​:

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-followup@​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 <rjbs@​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)

Implemented, and added a test in S19-command-line/arguments.t.

@p6rt p6rt closed this as completed Jul 15, 2015
@p6rt
Copy link
Author

p6rt commented Jul 15, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant