develooper Front page | perl.perl5.porters | Postings from September 2013

[perl #119555] require should not clobber $! and $^E on success

Thread Next
From:
Christian Millour
Date:
September 4, 2013 16:45
Subject:
[perl #119555] require should not clobber $! and $^E on success
Message ID:
rt-3.6.HEAD-1873-1378114129-1728.119555-75-0@perl.org
# New Ticket Created by  Christian Millour 
# Please include the string:  [perl #119555]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org:443/rt3/Ticket/Display.html?id=119555 >


This is a bug report for perl from cm.perl@abtela.com,
generated with the help of perlbug 1.39 running under perl 5.19.4.


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

On success, require resets $! and clobbers $^E (on platforms where $^E 
is distinct from $!, such as Windows). It would be better to restore the 
previous values of $! and $^E.

In the process of foraging through @INC, require may try to open several 
files, and when it succeeds on step N, $! and $^E carry the failure 
codes for step N-1. Such failures are however part of the normal working 
of require. There is no reason to report them to the user. This 
consideration might have motivated the resetting of $! (although the 
exact ratinale is unclear, see 
http://www.nntp.perl.org/group/perl.perl5.porters/2012/12/msg197015.html). 
In any case $^E was left alone, and for the sake of consistency we might 
consider resetting it also.

But resetting $! (and $^E) is a *bad thing* as it interferes with error 
reporting. A number of core and CPAN modules use the following idiom (or 
variants thereof) to implement 'lazy loading' of Carp :

sub croak {
   require Carp;
   goto &Carp::croak;
}

The problem here is that on first invocation (assuming Carp has not been 
loaded elsewhere), $! and $^E will have been corrupted to unusable 
values, as the following demonstrates :

Taisha:/cygdrive/d/perls/blead/perl-git/win32 $ cat 
e:/cm/perl/tests/badcarp/require_clobbering_errno.pl
use strict;
use warnings;

sub croak {
     require Carp;
     goto &Carp::croak;
}

for (1 .. 3) {
     eval {
	rmdir 'notempty'
	    or croak '$! = ', 0+$!, ', $^E = ', 0+$^E, "\n";
	1;
     } or do {
	my @msg = split /\n/, $@;
	print '$@  : "', $msg[0], '"', "\n";
	print '$!  : ', 0+$! , " ($!)\n";
	print '$^E : ', 0+$^E, " ($^E)\n";
	print "-----\n";
     }
}
Taisha:/cygdrive/d/perls/blead/perl-git/win32 $ ../perl 
e:/cm/perl/tests/badcarp/require_clobbering_errno.pl
$@  : "$! = 41, $^E = 145"
$!  : 0 ()
$^E : 6 (Descripteur non valide)
-----
$@  : "$! = 41, $^E = 145"
$!  : 41 (Directory not empty)
$^E : 145 (Le répertoire n'est pas vide)
-----
$@  : "$! = 41, $^E = 145"
$!  : 41 (Directory not empty)
$^E : 145 (Le répertoire n'est pas vide)
-----
Taisha:/cygdrive/d/perls/blead/perl-git/win32 $

Recent work on Carp (commit cbd58baf5927dd469f38f80a7c76c8011150b6c5) 
ensured that croak and confess hand out uncorrupted values of $! and 
$^E, thus giving access to their numerical values for programmatic error 
recovery (see 
http://www.nntp.perl.org/group/perl.perl5.porters/2013/08/msg206736.html 
and 
http://www.nntp.perl.org/group/perl.perl5.porters/2012/12/msg197017.html 
for a rationale).

In line with this effort, and considering the problem outlined above, 
require should do the right thing and restore on success the values of 
$! and $^E as they were on invocation.


ALTERNATIVE

If there is a compelling reason to keep having require corrupt^Hreset 
$!, then $^E should probably also be reset for consistency (and both 
resets should be documented). In addition, the idiom above should be 
recast as

sub croak {
   { local ($!, $^E); require Carp }
   goto &Carp::croak;
}

here is an indicative list of core files to patch :
dist/SelfLoader/lib/SelfLoader.pm
lib/POSIX.pm
lib/open.pm
lib/File/Compare.pm
lib/File/Copy.pm
lib/File/Path.pm
lib/attributes.pm
lib/_charnames.pm
lib/Tie/Memoize.pm
lib/SelfLoader.pm
lib/Term/Cap.pm
cpan/File-Path/lib/File/Path.pm
cpan/Term-Cap/Cap.pm
ext/attributes/attributes.pm
ext/POSIX/lib/POSIX.pm
ext/Tie-Memoize/lib/Tie/Memoize.pm

WORKAROUND

The problem above can be avoided by loading Carp as early as possible in 
the main script (which I have been doing for years, after having been 
bitten by this exact bug).

[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
     category=core
     severity=low
---
Site configuration information for perl 5.19.4:

Configured by cm at Mon Sep  2 01:34:49 2013.

Summary of my perl5 (revision 5 version 19 subversion 4) configuration:
   Commit id: cbd58baf5927dd469f38f80a7c76c8011150b6c5
   Platform:
     osname=MSWin32, osvers=4.0, 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='gcc', ccflags =' -s -O2 -DWIN32 -DWIN64 -DCONSERVATIVE 
-DPERL_TEXTMODE_SCRIPTS -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS 
-DUSE_PERLIO -fno-strict-aliasing -mms-bitfields',
     optimize='-s -O2',
     cppflags='-DWIN32'
     ccversion='', gccversion='4.6.3', gccosandvers=''
     intsize=4, longsize=4, ptrsize=8, doublesize=8, byteorder=12345678
     d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
     ivtype='long long', ivsize=8, nvtype='double', nvsize=8, 
Off_t='long long', lseeksize=8
     alignbytes=8, prototype=define
   Linker and Libraries:
     ld='g++', ldflags ='-s -L"d:\perls\blead\perl\lib\CORE" 
-L"C:\MinGW\lib"'
     libpth=C:\MinGW\lib
     libs=-lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 
-ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr 
-lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32
     perllibs=-lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool 
-lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid 
-lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32
     libc=, so=dll, useshrplib=true, libperl=libperl519.a
     gnulibc_version=''
   Dynamic Linking:
     dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
     cccdlflags=' ', lddlflags='-mdll -s 
-L"d:\perls\blead\perl\lib\CORE" -L"C:\MinGW\lib"'


---
@INC for perl 5.19.4:
     d:/perls/blead/perl/site/lib
     d:/perls/blead/perl/lib
     .

---
Environment for perl 5.19.4:
     CYGWIN=nodosfilewarning
     HOME=e:/cm
     LANG (unset)
     LANGUAGE (unset)
     LD_LIBRARY_PATH (unset)
     LOGDIR (unset)
 
PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\strawberry-perl-5.16.1.1-64bit-portable\c\bin
     PERL_BADLANG (unset)
     SHELL (unset)


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