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

Bug: File::Spec->path() doesn't keep trailing empty elements #22346

Closed
aarondill opened this issue Jun 25, 2024 · 9 comments · Fixed by #22403
Closed

Bug: File::Spec->path() doesn't keep trailing empty elements #22346

aarondill opened this issue Jun 25, 2024 · 9 comments · Fixed by #22403

Comments

@aarondill
Copy link
Contributor

aarondill commented Jun 25, 2024

Module: File::Spec

Description

Tested on Linux, but likely an issue on other platforms as well.
File::Spec->path() accidentally trims trailing empty elements in $PATH due to it's use of split() without a LIMIT.

Steps to Reproduce

> export PATH=/some/path/: # Note trailing colon
> /usr/bin/perl -e 'require File::Spec; print join(":", File::Spec->path());'
/some/path

> export PATH=:::/some/path/:::: # Leading colons are kept
> /usr/bin/perl -e 'require File::Spec; print join(":", File::Spec->path());'
.:.:.:/usr/bin/

> export PATH=: # This should be the same as '.'
> /usr/bin/perl -e 'require File::Spec; print join(":", File::Spec->path());'

> export PATH=:::::: # Technically all trailing
> /usr/bin/perl -e 'require File::Spec; print join(":", File::Spec->path());'

Expected behavior

File::Spec->path() should include trailing empty elements.

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
@aarondill
Copy link
Contributor Author

This bug is in ..../File/Spec/Unix.pm

my @path = split(':', $ENV{PATH});

Here, split should be passed a negative value for LIMIT: If LIMIT is negative, it is treated as if it were instead arbitrarily large; as many fields as possible are produced.

According to the POSIX Specifications for PATH, trailing colons are counted as empty elements: A zero-length prefix is a legacy feature that indicates the current working directory. It appears as two adjacent <colon> characters ( "::" ), as an initial <colon> preceding the rest of the list, or as a trailing <colon> following the rest of the list

@aarondill
Copy link
Contributor Author

aarondill commented Jun 25, 2024

I am willing to create a PR to fix this, however I'm not sure which other platforms this bug affects.
OS2.pm, Win32.pm, and Unix.pm all use split() to split the $PATH variable and as such are likely susceptible to this bug.

I'm not sure the conventions on Windows or OS2, so I am going to wait until I recieve feedback to start a PR.

@tonycoz
Copy link
Contributor

tonycoz commented Jun 26, 2024

Windows searches the current directory first by default anyway:

# argv.exe just dumps argv
C:\Users\Tony\dev\perl\git>copy argv.exe jar.exe
        1 file(s) copied.

C:\Users\Tony\dev\perl\git>jar
0: 'jar'

C:\Users\Tony\dev\perl\git>del jar.exe

C:\Users\Tony\dev\perl\git>jar
Usage: jar [OPTION...] [ [--release VERSION] [-C dir] files] ...
Try `jar --help' for more information.

jar.exe is in the first directory in PATH

So I don't think it matters on Win32.

@jkeenan
Copy link
Contributor

jkeenan commented Jul 12, 2024

I am willing to create a PR to fix this, however I'm not sure which other platforms this bug affects. OS2.pm, Win32.pm, and Unix.pm all use split() to split the $PATH variable and as such are likely susceptible to this bug.

I'm not sure the conventions on Windows or OS2, so I am going to wait until I recieve feedback to start a PR.

Based on @tonycoz's feedback, I recommend creating a pull request consisting of 2 commits.

The first commit should adjust that split line in lib/File/Spec/Unix.pm as you suggest. It should add tests, probably to dist/PathTools/t/Spec.t that would fail if run today but will pass after your code changes. This first commit should also increment $VERSION in all .pm files in the dist/PathTools/ tree in the core distribution (including Cwd.pm). Please make sure that all the tests in dist/PathTools/t/*.t PASS, and please make sure that make test_porting also PASSes.

The second commit should adjust the split line in lib/File/Spec/OS2.pm. I'm suggesting that you do that in a second commit because we probably don't have an OS2 available for testing and if we need to back that change out in the future, it will be easier to have it in a commit of its own.

@jkeenan
Copy link
Contributor

jkeenan commented Jul 12, 2024

Also, in the first of the two commits, please run perl Porting/updateAUTHORS.pl to add your name and preferred email address.

@aarondill
Copy link
Contributor Author

@jkeenan Should the second commit (changing OS2.pm) also increment $VERSION? Or should $VERSION only be incremented once?

@jkeenan
Copy link
Contributor

jkeenan commented Jul 14, 2024

@jkeenan Should the second commit (changing OS2.pm) also increment $VERSION? Or should $VERSION only be incremented once?

$VERSION should only be incremented once, and it should be the same in all *.pm files in the distribution.

@aarondill
Copy link
Contributor Author

Okay, I'll update them to match. Out of curiosity, why don't the $VERSION values match in bleed?

> git sw -q blead && grep -r 'our \$VERSION =' dist/PathTools/
dist/PathTools/Cwd.pm:our $VERSION = '3.90';
dist/PathTools/lib/File/Spec.pm:our $VERSION = '3.90';
dist/PathTools/lib/File/Spec/Cygwin.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/Epoc.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/VMS.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/Functions.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/AmigaOS.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/Win32.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/Mac.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/OS2.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/Unix.pm:our $VERSION = '3.91';

jkeenan added a commit that referenced this issue Jul 14, 2024
Corrects oversight in aa59296; spotted by Aaron Dill in GH #22346.
@jkeenan
Copy link
Contributor

jkeenan commented Jul 14, 2024

Okay, I'll update them to match. Out of curiosity, why don't the $VERSION values match in bleed?

> git sw -q blead && grep -r 'our \$VERSION =' dist/PathTools/
dist/PathTools/Cwd.pm:our $VERSION = '3.90';
dist/PathTools/lib/File/Spec.pm:our $VERSION = '3.90';
dist/PathTools/lib/File/Spec/Cygwin.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/Epoc.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/VMS.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/Functions.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/AmigaOS.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/Win32.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/Mac.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/OS2.pm:our $VERSION = '3.91';
dist/PathTools/lib/File/Spec/Unix.pm:our $VERSION = '3.91';

Error on our part. I have submitted #22404 to correct. Thanks for spotting that.

jkeenan added a commit that referenced this issue Jul 14, 2024
Corrects oversight in aa59296; spotted by Aaron Dill in GH #22346.
steve-m-hay pushed a commit that referenced this issue Oct 20, 2024
Corrects oversight in aa59296; spotted by Aaron Dill in GH #22346.

(cherry picked from commit 78facf5)
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

Successfully merging a pull request may close this issue.

3 participants