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

[perl #77336] Serious error with the handling of undef values

Thread Next
From:
Malcolm Hoar
Date:
August 20, 2010 10:05
Subject:
[perl #77336] Serious error with the handling of undef values
Message ID:
rt-3.6.HEAD-3821-1282316441-940.77336-75-0@perl.org
# New Ticket Created by  Malcolm Hoar 
# Please include the string:  [perl #77336]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=77336 >


This is a bug report for perl from malch@malch.com,

Operating System: 64-bit Windows 7.

Perl Version: ActivePerl version 5.12.1.1201 64-bit:

---
Flags:
     category=core
     severity=high
---

Code to reproduce this problem:

#====================================================

@Big1 = ();
$Big1[4] = 0;
$Big1[9] = 1;
$Big1[6] = 2;
my $len = 0; # This appears to be significant
foreach $key (@Big1) {
   $len = length ($key);
   print "key = $key, len=$len\n";
}
exit;

#=====================================================

Using ActivePerl version 5.12.1.1201 64-bit I get:

C:\ZIP>c:\perl512\bin\perl.exe test.pl
key = , len=0
key = , len=0
key = , len=0
key = , len=0
key = 0, len=1
key = , len=1     <==== ???
key = 2, len=1
key = , len=1     <==== ???
key = , len=1     <==== ???
key = 1, len=1

Using ActivePerl version 5.10.1.1007 64-bit I get:

C:\ZIP>C:\perl510\bin\perl.exe test.pl
key = , len=0
key = , len=0
key = , len=0
key = , len=0
key = 0, len=1
key = , len=0
key = 2, len=1
key = , len=0
key = , len=0
key = 1, len=1


Some other Perl programmers have taken the position that this
is a programmer error because the code attempts to establish
the length() of an undef value. That, they say, is garbage
analogous to division by zero.

I disagree and cite the following in support of my position
that this constitutes a bug:

1. Perl Version 5.10.1.1007 and every other major Perl version
    going back 10 plus years have consistently returned zero
    for the length of an undef value.

2. perlsyn clearly states:

    "A variable holds the undefined value ("undef") until it has been
    assigned a defined value, which is anything other than "undef".
    When used as a number, "undef" is treated as 0; when used as a
    string, it is treated as the empty string, ""; and when used as
    a reference that isn't being assigned to, it is treated as an error."

    Thus "undef" should be treated as the empty string. And the
    length of the empty string is unambiguously zero. Hence the
    behavior of the 5.12.1.1201 is clearly a bug!


Summary of my perl5 (revision 5 version 12 subversion 1) configuration:

   Platform:
     osname=MSWin32, osvers=5.2, archname=MSWin32-x64-multi-thread
     uname=''
     config_args='undef'
     hint=recommended, useposix=true, d_sigaction=undef
     useithreads=define, usemultiplicity=define
     useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
     use64bitint=define, use64bitall=undef, uselongdouble=undef
     usemymalloc=n, bincompat5005=undef
   Compiler:
     cc='cl', ccflags ='-nologo -GF -W3 -MD -Zi -DNDEBUG -Ox -GL -Wp64 
-fp:precis
e -DWIN32 -D_CONSOLE -DNO_STRICT -DHAVE_DES_FCRYPT -DWIN64 
-DCONSERVATIVE -DUSE_
SITECUSTOMIZE -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO 
-DPERL_MS
VCRT_READFIX',
     optimize='-MD -Zi -DNDEBUG -Ox -GL -Wp64 -fp:precise',
     cppflags='-DWIN32'
     ccversion='14.00.40310.41', gccversion='', gccosandvers=''
     intsize=4, longsize=4, ptrsize=8, doublesize=8, byteorder=12345678
     d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=8
     ivtype='__int64', ivsize=8, nvtype='double', nvsize=8, 
Off_t='__int64', lsee
ksize=8
     alignbytes=8, prototype=define
   Linker and Libraries:
     ld='link', ldflags ='-nologo -nodefaultlib -debug -opt:ref,icf 
-ltcg  -libpa
th:"C:\Perl512\lib\CORE"  -machine:AMD64'
     libpth=\lib
     libs=  oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib 
  comdlg32
.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib  netapi32.lib 
uuid.lib ws2_
32.lib mpr.lib winmm.lib  version.lib odbc32.lib odbccp32.lib 
comctl32.lib buffe
roverflowU.lib msvcrt.lib
     perllibs=  oldnames.lib kernel32.lib user32.lib gdi32.lib 
winspool.lib  comd
lg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib  netapi32.lib 
uuid.lib
ws2_32.lib mpr.lib winmm.lib  version.lib odbc32.lib odbccp32.lib 
comctl32.lib b
ufferoverflowU.lib msvcrt.lib
     libc=msvcrt.lib, so=dll, useshrplib=true, libperl=perl512.lib
     gnulibc_version=''
   Dynamic Linking:
     dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
     cccdlflags=' ', lddlflags='-dll -nologo -nodefaultlib -debug 
-opt:ref,icf -l
tcg  -libpath:"C:\Perl512\lib\CORE"  -machine:AMD64'


Characteristics of this binary (from libperl):
   Compile-time options: MULTIPLICITY PERL_DONT_CREATE_GVSV
                         PERL_IMPLICIT_CONTEXT PERL_IMPLICIT_SYS
                         PERL_MALLOC_WRAP PL_OP_SLAB_ALLOC USE_64_BIT_INT
                         USE_ITHREADS USE_LARGE_FILES USE_PERLIO 
USE_PERL_ATOF
                         USE_SITECUSTOMIZE
   Locally applied patches:
         ActivePerl Build 1201 [292674]
         d956618 Make Term::ReadLine::findConsole fall back to STDIN if 
/dev/tty
can't be opened
         321e50c Escape patch strings before embedding them in patchlevel.h
   Built under MSWin32
   Compiled at May 14 2010 00:22:26
   @INC:
     c:/Perl512/site/lib
     c:/Perl512/lib
     .

-- 
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
| Malcolm Hoar           "The more I practice, the luckier I get". |
| malch@malch.com                                     Gary Player. |
| http://www.malch.com/               Shpx gur PQN.                |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Thread Next


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