develooper Front page | perl.perl5.porters | Postings from August 2009

[perl #68640] Wrong error for undef constant name

From:
Nicholas Clark
Date:
August 18, 2009 04:10
Subject:
[perl #68640] Wrong error for undef constant name
Message ID:
rt-3.6.HEAD-2466-1250585583-617.68640-75-0@perl.org
# New Ticket Created by  Nicholas Clark 
# Please include the string:  [perl #68640]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=68640 >



This is a bug report for perl from nick@ccl4.org,
generated with the help of perlbug 1.39 running under perl 5.11.0.


-----------------------------------------------------------------
[Please describe your issue here]

$ ./perl -Ilib -we 'use constant undef, 1'
Use of uninitialized value within @_ in hash element at lib/constant.pm line 62.
Constant name '' is invalid at -e line 1
BEGIN failed--compilation aborted at -e line 1.


However, given that the code for constant looks like this:

    if ( $multiple ) {
	if (ref $_[0] ne 'HASH') {
	    require Carp;
	    Carp::croak("Invalid reference type '".ref(shift)."' not 'HASH'");
	}
	$constants = shift;
    } else {
	$constants->{+shift} = undef;
    }

    foreach my $name ( keys %$constants ) {
	unless (defined $name) {
	    require Carp;
	    Carp::croak("Can't use undef as constant name");
	}


it seems clear that the intent is that the error should be
"Can't use undef as constant name".

I can see two fixes. One is paranoid, one is fast.

The paranoid fix is to duplicate that C<defined $name> clause into 

	$constants->{+shift} = undef;


This is paranoid because it copes with someone passing in a tied hash that
is capable of returning undefined keys.

The "fast" version is simply to move the check. This gets it out of the loop,
but does mean that we can get tripped up by crazy fools with tied hashes.
But perfect is the enemy of the good - maybe the crazy fools get what they
deserve.

Nicholas Clark

[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
    category=library
    severity=low
    module=constant
---
Site configuration information for perl 5.11.0:

Configured by nick at Mon Aug 17 12:07:33 BST 2009.

Summary of my perl5 (revision 5 version 11 subversion 0) configuration:
  Local Commit: beb5337c331af7597cf571560e8b7f31c3a90450
  Ancestor: 5115136b5ada1a3245a69b04d93664e445e85eb1
  Platform:
    osname=linux, osvers=2.6.18-xenu, archname=x86_64-linux
    uname='linux zazen 2.6.18-xenu #1 smp thu oct 4 12:23:41 bst 2007 x86_64 gnulinux '
    config_args='-Dusedevel=y -Dcc=ccache gcc -Dld=gcc -Ubincompat5005 -Uinstallusrbinperl -Dcf_email=nick@ccl4.org -Dperladmin=nick@ccl4.org -Dinc_version_list=  -Dinc_version_list_init=0 -Doptimize=-g -Uusethreads -Uuse64bitall -Uusemymalloc -Duseperlio -Dprefix=~/Sandpit/snap5.9.x-GitLive-blead-1853-gbeb5337 -Uusevendorprefix -Uvendorprefix=~/Sandpit/snap5.9.x-GitLive-blead-1853-gbeb5337 -Dinstallman1dir=none -Dinstallman3dir=none -Uuserelocatableinc -de'
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=undef, usemultiplicity=undef
    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
    use64bitint=define, use64bitall=undef, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='ccache gcc', ccflags ='-DDEBUGGING -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-g',
    cppflags='-DDEBUGGING -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include'
    ccversion='', gccversion='4.3.2', gccosandvers=''
    intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='gcc', ldflags =' -fstack-protector -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib /lib64 /usr/lib64
    libs=-lnsl -ldb -ldl -lm -lcrypt -lutil -lc
    perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc
    libc=/lib/libc-2.7.so, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version='2.7'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -g -L/usr/local/lib -fstack-protector'

Locally applied patches:
    

---
@INC for perl 5.11.0:
    lib
    /home/nick/Sandpit/snap5.9.x-GitLive-blead-1853-gbeb5337/lib/perl5/site_perl/5.11.0/x86_64-linux
    /home/nick/Sandpit/snap5.9.x-GitLive-blead-1853-gbeb5337/lib/perl5/site_perl/5.11.0
    /home/nick/Sandpit/snap5.9.x-GitLive-blead-1853-gbeb5337/lib/perl5/5.11.0/x86_64-linux
    /home/nick/Sandpit/snap5.9.x-GitLive-blead-1853-gbeb5337/lib/perl5/5.11.0
    .

---
Environment for perl 5.11.0:
    HOME=/home/nick
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/home/nick/bin:/usr/local/bin:/usr/bin:/bin:/usr/games:/usr/local/sbin:/sbin:/usr/sbin
    PERL_BADLANG (unset)
    SHELL=/bin/bash




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About