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

Wrong error for undef constant name #9836

Closed
p5pRT opened this issue Aug 18, 2009 · 7 comments
Closed

Wrong error for undef constant name #9836

p5pRT opened this issue Aug 18, 2009 · 7 comments

Comments

@p5pRT
Copy link

p5pRT commented Aug 18, 2009

Migrated from rt.perl.org#68640 (status was 'resolved')

Searchable as RT68640$

@p5pRT
Copy link
Author

p5pRT commented Aug 18, 2009

From @nwc10

Created by @nwc10

$ ./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

Perl Info

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

@p5pRT
Copy link
Author

p5pRT commented Nov 12, 2009

@xdg - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Dec 14, 2009

From zefram@fysh.org

Attached patch fixes bug #68640.

-zefram

@p5pRT
Copy link
Author

p5pRT commented Dec 14, 2009

From zefram@fysh.org

Inline Patch
diff --git a/dist/constant/lib/constant.pm b/dist/constant/lib/constant.pm
index a51ee7f..3ee1a6f 100644
--- a/dist/constant/lib/constant.pm
+++ b/dist/constant/lib/constant.pm
@@ -4,7 +4,7 @@ use strict;
 use warnings::register;
 
 use vars qw($VERSION %declared);
-$VERSION = '1.19';
+$VERSION = '1.20';
 
 #=======================================================================
 
@@ -60,15 +60,14 @@ sub import {
 	}
 	$constants = shift;
     } else {
-	$constants->{+shift} = undef;
-    }
-
-    foreach my $name ( keys %$constants ) {
-	unless (defined $name) {
+	unless (defined $_[0]) {
 	    require Carp;
 	    Carp::croak("Can't use undef as constant name");
 	}
+	$constants->{+shift} = undef;
+    }
 
+    foreach my $name ( keys %$constants ) {
 	# Normal constant name
 	if ($name =~ $normal_constant_name and !$forbidden{$name}) {
 	    # Everything is okay
diff --git a/dist/constant/t/constant.t b/dist/constant/t/constant.t
index a42b7d2..85a9355 100644
--- a/dist/constant/t/constant.t
+++ b/dist/constant/t/constant.t
@@ -9,7 +9,7 @@ END { @warnings && print STDERR join "\n- ", "accumulated warnings:", @warnings
 
 
 use strict;
-use Test::More tests => 95;
+use Test::More tests => 96;
 my $TB = Test::More->builder;
 
 BEGIN { use_ok('constant'); }
@@ -341,3 +341,9 @@ $kloong = 'schlozhauer';
     is ($@, '');
     is_deeply (\@value, []);
 }
+
+{
+    local $SIG{'__WARN__'} = sub { die "WARNING: $_[0]" };
+    eval 'use constant undef, 5; 1';
+    like $@, qr/\ACan't use undef as constant name at /;
+}

@p5pRT
Copy link
Author

p5pRT commented Dec 15, 2009

From @rgarcia

2009/12/14 Zefram <zefram@​fysh.org>​:

Attached patch fixes bug #68640.

Thanks, applied to bleadperl. cc​:ing Sébastien for the CPAN release.

@p5pRT
Copy link
Author

p5pRT commented Dec 15, 2009

@rgs - Status changed from 'open' to 'resolved'

@p5pRT p5pRT closed this as completed Dec 15, 2009
@p5pRT
Copy link
Author

p5pRT commented Apr 17, 2011

From @maddingue

Rafael Garcia-Suarez wrote​:

2009/12/14 Zefram <zefram@​fysh.org>​:

Attached patch fixes bug #68640.

Thanks, applied to bleadperl. cc​:ing Sébastien for the CPAN release.

With apologies for the delay, constant 0.20 is now uploaded on the
CPAN, sponsored by the Perl QA Hackathon 2011 in Amsterdam.

--
Sébastien Aperghis-Tramoni

Close the world, txEn eht nepO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant