Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.9k stars 540 forks source link

Give more informative error when using a filehandle as a directory handle #22394

Closed jessesheidlower closed 1 month ago

jessesheidlower commented 1 month ago

Module:

Description If you mistakenly call open (rather than opendir) on a directory, and then try to read from what you thought was a directory handle, you will get an unclear error. This could be improved.

(This was inspired by this Perlmonks discussion.)

Steps to Reproduce

#!/usr/bin/env perl                                                         

 use strict;                                                                 
 use warnings;                                                               

 open (my $dh, "/etc") or die "Could not open dir for reading: $!\n"; # should be opendir  
 my @files = readdir $dh; # this is actually a file handle but coder thought it was a directory handle
 print @files;

output:

readdir() attempted on invalid dirhandle $dh at test.pl line 7.

Expected behavior A not-unreasonable expectation would be an error or warning when trying to call open on a directory, but it seems like there's nothing intrinsically invalid about doing this. However, when calling readdir on a filehandle, perhaps a better error could be reported.

Perl configuration

Summary of my perl5 (revision 5 version 38 subversion 2) configuration:

  Platform:
    osname=linux
    osvers=5.12.15-arch1-1
    archname=x86_64-linux-thread-multi
    uname='archlinux'
    config_args='-des -Dusethreads -Duseshrplib -Doptimize=-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions         -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security         -fstack-clash-protection -fcf-protection -g -ffile-prefix-map=/build/perl/src=/usr/src/debug/perl -flto=auto -Dprefix=/usr -Dvendorprefix=/usr -Dprivlib=/usr/share/perl5/core_perl -Darchlib=/usr/lib/perl5/5.38/core_perl -Dsitelib=/usr/share/perl5/site_perl -Dsitearch=/usr/lib/perl5/5.38/site_perl -Dvendorlib=/usr/share/perl5/vendor_perl -Dvendorarch=/usr/lib/perl5/5.38/vendor_perl -Dscriptdir=/usr/bin/core_perl -Dsitescript=/usr/bin/site_perl -Dvendorscript=/usr/bin/vendor_perl -Dinc_version_list=none -Dman1ext=1perl -Dman3ext=3perl -Dlddlflags=-shared -Wl,-O1 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now          -Wl,-z,pack-relative-relocs -flto=auto -Dldflags=-Wl,-O1 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now          -Wl,-z,pack-relative-relocs -flto=auto -Dloclibpth=/usr/lib/db5.3 -Dlocincpth=/usr/include/db5.3'
    hint=recommended
    useposix=true
    d_sigaction=define
    useithreads=define
    usemultiplicity=define
    use64bitint=define
    use64bitall=define
    uselongdouble=undef
    usemymalloc=n
    default_inc_excludes_dot=define
  Compiler:
    cc='cc'
    ccflags ='-D_REENTRANT -D_GNU_SOURCE -fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/include/db5.3 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64'
    optimize='-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -g -ffile-prefix-map=/build/perl/src=/usr/src/debug/perl -flto=auto'
    cppflags='-D_REENTRANT -D_GNU_SOURCE -fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/include/db5.3'
    ccversion=''
    gccversion='13.2.1 20230801'
    gccosandvers=''
    intsize=4
    longsize=8
    ptrsize=8
    doublesize=8
    byteorder=12345678
    doublekind=3
    d_longlong=define
    longlongsize=8
    d_longdbl=define
    longdblsize=16
    longdblkind=3
    ivtype='long'
    ivsize=8
    nvtype='double'
    nvsize=8
    Off_t='off_t'
    lseeksize=8
    alignbytes=8
    prototype=define
  Linker and Libraries:
    ld='cc'
    ldflags ='-Wl,-O1 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,-z,pack-relative-relocs -flto=auto -fstack-protector-strong -L/usr/lib/db5.3'
    libpth=/usr/local/lib /usr/lib /usr/lib/db5.3
    libs=-lpthread -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc -lgdbm_compat
    perllibs=-lpthread -ldl -lm -lcrypt -lutil -lc
    libc=/lib/../lib/libc.so.6
    so=so
    useshrplib=true
    libperl=libperl.so
    gnulibc_version='2.39'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs
    dlext=so
    d_dlsymun=undef
    ccdlflags='-Wl,-E -Wl,-rpath,/usr/lib/perl5/5.38/core_perl/CORE'
    cccdlflags='-fPIC'
    lddlflags='-shared -Wl,-O1 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,-z,pack-relative-relocs -flto=auto -L/usr/lib/db5.3 -fstack-protector-strong'

Characteristics of this binary (from libperl): 
  Compile-time options:
    HAS_LONG_DOUBLE
    HAS_STRTOLD
    HAS_TIMES
    MULTIPLICITY
    PERLIO_LAYERS
    PERL_COPY_ON_WRITE
    PERL_DONT_CREATE_GVSV
    PERL_HASH_FUNC_SIPHASH13
    PERL_HASH_USE_SBOX32
    PERL_MALLOC_WRAP
    PERL_OP_PARENT
    PERL_PRESERVE_IVUV
    PERL_USE_SAFE_PUTENV
    USE_64_BIT_ALL
    USE_64_BIT_INT
    USE_ITHREADS
    USE_LARGE_FILES
    USE_LOCALE
    USE_LOCALE_COLLATE
    USE_LOCALE_CTYPE
    USE_LOCALE_NUMERIC
    USE_LOCALE_TIME
    USE_PERLIO
    USE_PERL_ATOF
    USE_REENTRANT_API
    USE_THREAD_SAFE_LOCALE
  Built under linux
  Compiled at Feb 11 2024 19:15:41
  @INC:
    /usr/lib/perl5/5.38/site_perl
    /usr/share/perl5/site_perl
    /usr/lib/perl5/5.38/vendor_perl
    /usr/share/perl5/vendor_perl
    /usr/lib/perl5/5.38/core_perl
    /usr/share/perl5/core_perl
jkeenan commented 1 month ago

Given that the two-argument form to open is indifferent as to the spelling of any variable provided as the first argument ...

$ perl -e 'open (my $dh, "/etc") or die "Could not open dir for reading: $!\n";'
[# no output ]
$ touch /tmp/foo
$ perl -e 'open (my $dh, "/tmp/foo") or die "Could not open dir for reading: $!\n";'
[# no output ]
$ mkdir -p /tmp/bar
$ perl -e 'open (my $dh, "/tmp/bar") or die "Could not open dir for reading: $!\n";'
[# no output ]

... open has no way of determining what a user's intent actually was.

So that leaves only the possibility of providing a more specific error warning for readdir.

$ perl -we 'open (my $dh, "/tmp/foo") or die "Could not open dir for reading: $!\n"; my @files = readdir($dh);'
readdir() attempted on invalid dirhandle $dh at -e line 1.

or, without warnings, but checking for success of readdir:

$ perl -e 'open (my $dh, "/tmp/foo") or die "Could not open dir for reading: $!\n"; my @files = readdir($dh) or die($!);'
Inappropriate ioctl for device at -e line 1.

I'm not persuaded that the readdir warning message is insufficient.

hvds commented 1 month ago

I'm not persuaded that the readdir warning message is insufficient.

Well we have at least one example of a user for whom it wasn't sufficient.

I'm not sure if the full glob or just the dirhandle is available at this point, but if it's possible I think it would be helpful to have a "did you use open() instead of opendir()" style message when the glob has an open filehandle.

(If we have only the dirhandle, perhaps there's a way to find back the (likely) glob from it, though I appreciate that's likely to be both harder and less canonical.)

richardleach commented 1 month ago

I'm not sure if the full glob or just the dirhandle is available at this point,

On the face of it, looks like the glob should be available and it should be possible to test for IoIFP(sv): https://github.com/richardleach/perl5/blob/blead/pp_sys.c#L4314