Skip to content

Setting $0 on macOS results in environment variables being included in the command name #20399

Open
@grr

Description

@grr

Description

Setting $0 on macOS results in the command as returned by ps including environment variables. The below script will produce output like:

...
/tmp/a.pl 10/10
COMMAND
/tmp/a.pl 10/1 PERLBREW_SHELLRC_VERSION=0.94

Note the modified command name is truncated and the environment variable is added. That is the first listed variable when running env. Running with any number of command-line arguments > 0 (ex. /tmp/a.pl a b c) prevents
the truncation of the command and doesn't print the environment variable.
However, using the system perl (threaded 5.30.2) works fine with only 0 command-line arguments. With
each additional command-line argument, the command name will contain an additional environment
variable.

https://perldoc.perl.org/perlvar#$0 documents many limitations in setting $0, but this is not mentioned. And running ps auxw doesn't show any other process that includes an environment variable. Perl >= 5.8 exhibit this issue.

Steps to Reproduce

Script:

#!/usr/bin/env perl
use strict;
use warnings;

my @range = (1..10);
for my $i (@range) {
    local $0 = "$0 $i/" . scalar @range;
    print "$0\n";
    print `ps -p $$ -o command`;
    sleep 1;
}

Expected behavior

Setting $0 should not result in including environment variables in the command name.

Perl configuration

Summary of my perl5 (revision 5 version 36 subversion 0) configuration:
   
  Platform:
    osname=darwin
    osvers=20.6.0
    archname=darwin-2level
    uname='darwin iphone 20.6.0 darwin kernel version 20.6.0: tue feb 22 21:10:42 pst 2022; root:xnu-7195.141.26~1release_arm64_t8101 arm64 '
    config_args='-de -Dprefix=/Users/grr/local/perlbrew/perls/perl-5.36 -Aeval:scriptdir=/Users/grr/local/perlbrew/perls/perl-5.36/bin'
    hint=recommended
    useposix=true
    d_sigaction=define
    useithreads=undef
    usemultiplicity=undef
    use64bitint=define
    use64bitall=define
    uselongdouble=undef
    usemymalloc=n
    default_inc_excludes_dot=define
  Compiler:
    cc='cc'
    ccflags ='-fno-common -DPERL_DARWIN -mmacosx-version-min=11.6 -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include -I/opt/local/include -DPERL_USE_SAFE_PUTENV'
    optimize='-O3'
    cppflags='-fno-common -DPERL_DARWIN -mmacosx-version-min=11.6 -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include -I/opt/local/include'
    ccversion=''
    gccversion='Apple LLVM 13.0.0 (clang-1300.0.29.30)'
    gccosandvers=''
    intsize=4
    longsize=8
    ptrsize=8
    doublesize=8
    byteorder=12345678
    doublekind=3
    d_longlong=define
    longlongsize=8
    d_longdbl=define
    longdblsize=8
    longdblkind=0
    ivtype='long'
    ivsize=8
    nvtype='double'
    nvsize=8
    Off_t='off_t'
    lseeksize=8
    alignbytes=8
    prototype=define
  Linker and Libraries:
    ld='cc'
    ldflags =' -mmacosx-version-min=11.6 -fstack-protector-strong -L/usr/local/lib -L/opt/local/lib'
    libpth=/usr/local/lib /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.0.0/lib /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib /opt/local/lib /usr/lib
    libs=-lgdbm
    perllibs=
    libc=
    so=dylib
    useshrplib=false
    libperl=libperl.a
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_dlopen.xs
    dlext=bundle
    d_dlsymun=undef
    ccdlflags=' '
    cccdlflags=' '
    lddlflags=' -mmacosx-version-min=11.6 -bundle -undefined dynamic_lookup -L/usr/local/lib -L/opt/local/lib -fstack-protector-strong'


Characteristics of this binary (from libperl): 
  Compile-time options:
    HAS_TIMES
    PERLIO_LAYERS
    PERL_COPY_ON_WRITE
    PERL_DONT_CREATE_GVSV
    PERL_MALLOC_WRAP
    PERL_OP_PARENT
    PERL_PRESERVE_IVUV
    PERL_USE_SAFE_PUTENV
    USE_64_BIT_ALL
    USE_64_BIT_INT
    USE_LARGE_FILES
    USE_LOCALE
    USE_LOCALE_COLLATE
    USE_LOCALE_CTYPE
    USE_LOCALE_NUMERIC
    USE_LOCALE_TIME
    USE_PERLIO
    USE_PERL_ATOF
  Built under darwin
  Compiled at May 27 2022 21:14:58
  %ENV:
    PERL5LIB="/Users/grr/local/perl5"
    PERLBREW_HOME="/Users/grr/.perlbrew"
    PERLBREW_MANPATH="/Users/grr/local/perlbrew/perls/current/man"
    PERLBREW_PATH="/Users/grr/local/perlbrew/bin:/Users/grr/local/perlbrew/perls/current/bin"
    PERLBREW_PERL="current"
    PERLBREW_ROOT="/Users/grr/local/perlbrew"
    PERLBREW_SHELLRC_VERSION="0.94"
    PERLBREW_VERSION="0.94"
    PERL_CPANM_OPT="-q --mirror https://www.cpan.org --mirror https://cpan.metacpan.org"
  @INC:
    /Users/grr/local/perl5
    /Users/grr/local/perlbrew/perls/perl-5.36/lib/site_perl/5.36.0/darwin-2level
    /Users/grr/local/perlbrew/perls/perl-5.36/lib/site_perl/5.36.0
    /Users/grr/local/perlbrew/perls/perl-5.36/lib/5.36.0/darwin-2level
    /Users/grr/local/perlbrew/perls/perl-5.36/lib/5.36.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions