develooper Front page | perl.perl5.porters | Postings from March 2003

[perl #21549] PerlIO+FileHandle+Term::ReadLine::Gnu causes segmentation fault

From:
Hiroo Hayashi
Date:
March 12, 2003 11:28
Subject:
[perl #21549] PerlIO+FileHandle+Term::ReadLine::Gnu causes segmentation fault
Message ID:
rt-21549-53577.5.48916813898316@bugs6.perl.org
# New Ticket Created by  Hiroo Hayashi 
# Please include the string:  [perl #21549]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=21549 >


This is a bug report for perl from hiroo.hayashi@computer.org
generated with the help of perlbug 1.34 running under perl v5.8.0.


-----------------------------------------------------------------
[Please enter your report here]

I'm maintaining Term::ReadLine::Gnu (TRG) and had some reports of
segmentation fault.

It seems that the following condition causes segmentation fault;
	1.perl 5.8.0 w/PerlIO enabled
	2.FileHandle	
	3.Term::Readline::Gnu (version 1.13)
For example CPAN.pm is one of the case.

Here is a short script which causes the segmentation fault.

========================================================================
Perl 5.8.0 w/PerlIO causes segmentation fault on this script.
========================================================================
#!/usr/bin/perl -w
use FileHandle;
use Math::FFT;
use Term::ReadLine;
my $term = new Term::ReadLine 'shell';
$_ = $term->readline('>');
========================================================================

A reporter who send me this script open a bug report on;
	http://rt.perl.org/rt2//Ticket/Display.html?id=20793
It seems that Perl_IO_close() is called unnecessarily.

And I found the following patch for typemap stopped the segmentation
fault.  The typemap for 'FILE *' (T_STDIO) in Term::ReadLine::Gnu-1.13
is exactly same as one in typemap in the Perl 5.8.0 distribution.

========================================================================
patch on typemap
========================================================================
--- typemap	2002-03-29 22:47:48-05	1.6
+++ typemap	2003-03-12 00:05:05-05
@@ -1,6 +1,6 @@
 #	typemap for Term::ReadLine::Gnu
 #
-#	$Id: typemap,v 1.6 2002-03-29 22:47:48-05 hiroo Exp $
+#	$Id: typemap,v 1.6 2002/03/30 03:47:48 hiroo Exp $
 
 const char *	T_PV
 CONST char *	T_PV
@@ -21,10 +21,10 @@
 OUTPUT
 T_STDIO
 	{
-	    GV *gv = newGVgen("$Package");
+	    GV *gv = newGVgen("IO::Handle");
 	    PerlIO *fp = PerlIO_importFILE($var,0);
 	    if ( fp && do_open(gv, "+<&", 3, FALSE, 0, 0, fp) )
-		sv_setsv($arg, sv_bless(newRV((SV*)gv), gv_stashpv("$Package",1)));
+		sv_setsv($arg, sv_bless(newRV((SV*)gv), gv_stashpv("IO::Handle",1)));
 	    else
 		$arg = &PL_sv_undef;
 	}
========================================================================

A few months ago another user sent me a report saying a script which
depended a strange behavior of FileHandle module fails.

========================================================================
Strange behavior of FileHandle
========================================================================
#!/usr/bin/perl
use FileHandle;
use Term::ReadLine;

my $term = new Term::ReadLine 'test';
my $OUT = $term->OUT;
print $OUT "Using " . $term->ReadLine ."\n";
# Term::ReadLine::Gnu without the patch fails on next line
$OUT->print("Using " . $term->ReadLine ."\n");
========================================================================

	PERL_RL=Stub perl this_script	# without TRL
does not fail, but
	PERL_RL=Gnu perl this_script	# with TRL
fails.

========================================================================
(herbie) hiroo[517] PERL_RL=Gnu perl /tmp/test.pl 
Using Term::ReadLine::Gnu
Can't locate object method "print" via package "Term::ReadLine::Gnu::Var" (perhaps you forgot to load "Term::ReadLine::Gnu::Var"?) at /tmp/test.pl line 9.
========================================================================

I think the last line *should* fail, because $OUT is not an object of
FileHandle class.  It seems that `use FileHandle' makes all Perl IO
handle be its objects automatically.

    Question 1:
	Is this a bug of Perl or FileHandle?

By using the patch for typemap, the script does not cause fail.


Considering this, I set up the follwoing hypothesis;

The first script, which causes segmentation fault, calls `use
FileHandle'.  This makes the file handle returned by TRL as a object
of FileHandle class.  When the script exits, the destructor of
FileHandle class is called.  The destructor calls Perl_IO_close().
But the file handler returned by TRL is not ininitalized properly
without the typemap patch, and Perl_IO_close() are called too many
times.

I'm not sure how correct this hypothesis is.  Any way,

    Question 2:
	I'm thinking to release Term::ReadLine::Gnu-1.14 including the
	typemap patch.  Are there any better fix?

All XS module which uses 'FILE *' typemap in Perl 5.8.0 should have
some problem.  If my patch is reasonable, the typemap in Perl 5.8.0
should be fixed, too.

[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
    category=core
    severity=medium
---
Site configuration information for perl v5.8.0:

Configured by hayashi at Tue Mar 11 23:31:21 CST 2003.

Summary of my perl5 (revision 5.0 version 8 subversion 0) configuration:
  Platform:
    osname=linux, osvers=2.4.20, archname=i686-linux
    uname='linux herbie 2.4.20 #1 tue mar 11 00:37:10 cst 2003 i686 unknown '
    config_args='-de -Dprefix=/usr/local/perl'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O3',
    cppflags='-fno-strict-aliasing -I/usr/local/include'
    ccversion='', gccversion='2.95.4 20011002 (Debian prerelease)', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -lgdbm -ldbm -ldb -ldl -lm -lc -lcrypt -lutil
    perllibs=-lnsl -ldl -lm -lc -lcrypt -lutil
    libc=/lib/libc-2.2.5.so, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version='2.2.5'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:
    

---
@INC for perl v5.8.0:
    /usr/local/perl/lib/5.8.0/i686-linux
    /usr/local/perl/lib/5.8.0
    /usr/local/perl/lib/site_perl/5.8.0/i686-linux
    /usr/local/perl/lib/site_perl/5.8.0
    /usr/local/perl/lib/site_perl/5.6.1
    /usr/local/perl/lib/site_perl/5.6.0
    /usr/local/perl/lib/site_perl/5.005
    /usr/local/perl/lib/site_perl
    .

---
Environment for perl v5.8.0:
    HOME=/home/hayashi
    LANG=C
    LANGUAGE (unset)
    LC_COLLATE=C
    LC_CTYPE=
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/home/hayashi/bin:/usr/local/gnu/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin:/usr/bin/X11
    PERL_BADLANG (unset)
    SHELL=/usr/local/gnu/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