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

Misleading warning "Use of uninitialized value $x in numeric eq (==)" when comparing value of $x from core function #21930

Closed
dboehmer opened this issue Feb 6, 2024 · 4 comments

Comments

@dboehmer
Copy link
Contributor

dboehmer commented Feb 6, 2024

Module: —

Description
I am comparing the length of a string. It happened to be undefined but the warning was misleading and made me think the logic of my comparison was broken. The warning reads like my code was missing parenthesis and compiled into length( $x == 0 ) which it didn’t.

I think this was probably discussed before and I am not the first to bring this up but googling this yields a ton of posts about low quality code which produced the warning and users trying to figure out its basic meaning.

Steps to Reproduce

$ perl -wE 'my $x; say( (length $x) == 0 )'
Use of uninitialized value $x in numeric eq (==) at -e line 1.
1

The warning is incorrect: not $x is compared but length($x). I confirmed this behavior for

  • abs($x)
  • length($x)
  • delete() on hash keys:
$ perl -wE 'my %h; say( delete $h{foo} == 0 )'
Use of uninitialized value $h{"foo"} in numeric eq (==) at -e line 1.
1

Expected behavior

It’s probably hard to implement a generic representation of the expression. So any of those:

  1. most informative: Use of uninitialized value length($x) in numeric eq (==) (I think, it might be possible as it looks like there is already special handling for core functions.)
  2. more precise: Use of uninitialized value $x in length
  3. just Use of uninitialized value in numeric eq (==) like for user-defined functions:
$ perl -wE 'sub f{shift} my $x; say( f($x) == 0 )'
Use of uninitialized value in numeric eq (==) at -e line 1.
1

Perl configuration

Summary of my perl5 (revision 5 version 38 subversion 0) configuration:
   
  Platform:
    osname=linux
    osvers=5.10.0-25-amd64
    archname=x86_64-linux-thread-multi
    uname='linux berlin 5.10.0-25-amd64 #1 smp debian 5.10.191-1 (2023-08-16) x86_64 gnulinux '
    config_args='-de -Dprefix=/home/daniel/perl5/perlbrew/perls/perl-5.38.0-threads-bullseye -Dusethreads -Aeval:scriptdir=/home/daniel/perl5/perlbrew/perls/perl-5.38.0-threads-bullseye/bin'
    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/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_FORTIFY_SOURCE=2'
    optimize='-O2'
    cppflags='-D_REENTRANT -D_GNU_SOURCE -fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include'
    ccversion=''
    gccversion='10.2.1 20210110'
    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 =' -fstack-protector-strong -L/usr/local/lib'
    libpth=/usr/local/lib /usr/lib/x86_64-linux-gnu /usr/lib /usr/lib64
    libs=-lpthread -ldl -lm -lcrypt -lutil -lc
    perllibs=-lpthread -ldl -lm -lcrypt -lutil -lc
    libc=libc-2.31.so
    so=so
    useshrplib=false
    libperl=libperl.a
    gnulibc_version='2.31'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs
    dlext=so
    d_dlsymun=undef
    ccdlflags='-Wl,-E'
    cccdlflags='-fPIC'
    lddlflags='-shared -O2 -L/usr/local/lib -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 Sep  1 2023 12:51:38
  %ENV:
    PERLBREW_HOME="/home/daniel/.perlbrew"
    PERLBREW_MANPATH="/home/daniel/perl5/perlbrew/perls/perl-5.38.0-threads-bullseye/man"
    PERLBREW_PATH="/home/daniel/perl5/perlbrew/bin:/home/daniel/perl5/perlbrew/perls/perl-5.38.0-threads-bullseye/bin"
    PERLBREW_PERL="perl-5.38.0-threads-bullseye"
    PERLBREW_ROOT="/home/daniel/perl5/perlbrew"
    PERLBREW_SHELLRC_VERSION="0.92"
    PERLBREW_VERSION="0.92"
  @INC:
    /home/daniel/perl5/perlbrew/perls/perl-5.38.0-threads-bullseye/lib/site_perl/5.38.0/x86_64-linux-thread-multi
    /home/daniel/perl5/perlbrew/perls/perl-5.38.0-threads-bullseye/lib/site_perl/5.38.0
    /home/daniel/perl5/perlbrew/perls/perl-5.38.0-threads-bullseye/lib/5.38.0/x86_64-linux-thread-multi
    /home/daniel/perl5/perlbrew/perls/perl-5.38.0-threads-bullseye/lib/5.38.0
@dboehmer
Copy link
Contributor Author

dboehmer commented Feb 6, 2024

This is also an interesting example which seems like handcrafted:

$ perl -wE 'my @a; say( shift(@a) == 0 )'
Use of uninitialized value within @a in numeric eq (==) at -e line 1.
1

mauke added a commit to mauke/perl5 that referenced this issue Feb 6, 2024
When perl looks for the source of "uninitialized value" warnings, it
treats length() as transparent: length(X) is undef iff X is undef, so it
makes sense to ignore length() when looking for the origin of the undef
value. However, this makes for misleading diagnostics: If $x is undef,
then 'length($x) == 0' triggers a "Use of uninitialized value $x in
numeric eq (==)" warning. This is correct insofar as (==) encountered an
undef value and $x is its origin, but incorrect in that $x is not one of
the operands of (==) in the source code.

This patch skips over length() when looking for the origin of undef (as
before), but adds "length(...)" around the name of the variable found,
if any.

Before:

    $ perl -we 'my $x; $_ = length $x == 0'
    Use of uninitialized value $x in numeric eq (==) at -e line 1.

(Misleading: $x is not an operand of (==).)

After:

    $ perl -we 'my $x; $_ = length $x == 0'
    Use of uninitialized value length($x) in numeric eq (==) at -e line 1.

See also: Perl#21930
@mauke
Copy link
Contributor

mauke commented Feb 6, 2024

I've pushed a potential fix in mauke@f5862f5.
I'm not sure if this is the right approach. It does seem to produce sensible diagnostics, though:

$ ./perl -we 'my $x = length length == 0'
Use of uninitialized value length(length($_)) in numeric eq (==) at -e line 1.

@Leont
Copy link
Contributor

Leont commented Feb 6, 2024

more precise: Use of uninitialized value $x in length

This would not be an option IMO, length on an undefined value is fine and should pass on the undefined value.

@Leont
Copy link
Contributor

Leont commented Feb 6, 2024

I've pushed a potential fix in mauke@f5862f5.
I'm not sure if this is the right approach. It does seem to produce sensible diagnostics, though:

That looks sensible to me.

mauke added a commit to mauke/perl5 that referenced this issue Feb 7, 2024
When perl looks for the source of "uninitialized value" warnings, it
treats length() as transparent: length(X) is undef iff X is undef, so it
makes sense to ignore length() when looking for the origin of the undef
value. However, this makes for misleading diagnostics: If $x is undef,
then 'length($x) == 0' triggers a "Use of uninitialized value $x in
numeric eq (==)" warning. This is correct insofar as (==) encountered an
undef value and $x is its origin, but incorrect in that $x is not one of
the operands of (==) in the source code.

This patch skips over length() when looking for the origin of undef (as
before), but adds "length(...)" around the name of the variable found,
if any.

Before:

    $ perl -we 'my $x; $_ = length $x == 0'
    Use of uninitialized value $x in numeric eq (==) at -e line 1.

(Misleading: $x is not an operand of (==).)

After:

    $ perl -we 'my $x; $_ = length $x == 0'
    Use of uninitialized value length($x) in numeric eq (==) at -e line 1.

Fixes Perl#21930.
@mauke mauke closed this as completed in 4a414ab Feb 8, 2024
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

4 participants