develooper Front page | perl.perl5.porters | Postings from February 2008

[perl #51092] [PATCH] Segfault when calling ->next::method on non-existing package

Thread Next
From:
ilmari @ vesla . ilmari . org
Date:
February 22, 2008 03:10
Subject:
[perl #51092] [PATCH] Segfault when calling ->next::method on non-existing package
Message ID:
rt-3.6.HEAD-15287-1203654581-377.51092-75-0@perl.org
# New Ticket Created by  ilmari@vesla.ilmari.org 
# Please include the string:  [perl #51092]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=51092 >



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


-----------------------------------------------------------------

When calling ->next::can on a package name that hasn't been defined at
all, perl segfaults when it tries to do HvNAME_get(NULL).

Here's a patch with a test. I'm not entirely sure whether it should die
or just return undef if !throw_nomethod (like UNIVERSAL->can does).

 mro.c                  |    5 ++++-
 t/mro/next_edgecases.t |   14 +++++++++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/mro.c b/mro.c
index 83872dc..be9a5d4 100644
--- a/mro.c
+++ b/mro.c
@@ -953,8 +953,11 @@ XS(XS_mro_nextcan)
 
     if(sv_isobject(self))
         selfstash = SvSTASH(SvRV(self));
-    else
+    else {
         selfstash = gv_stashsv(self, 0);
+        if(!selfstash)
+            Perl_croak(aTHX_ "Can't call next::method/next::can/maybe::next::method on undefined package \"%s\".", SvPV_nolen_const(self));
+    }
 
     assert(selfstash);
 
diff --git a/t/mro/next_edgecases.t b/t/mro/next_edgecases.t
index 91c2c85..9b872b6 100644
--- a/t/mro/next_edgecases.t
+++ b/t/mro/next_edgecases.t
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-require q(./test.pl); plan(tests => 11);
+require q(./test.pl); plan(tests => 12);
 
 {
 
@@ -79,4 +79,16 @@ require q(./test.pl); plan(tests => 11);
         eval { $baz->bar() };
         ok($@, '... calling bar() with next::method failed') || diag $@;
     }    
+
+    # Test it failing (instead of segfaulting) with non-existing class
+    {
+        package Qux;
+        use mro;
+        sub foo { No::Such::Class->next::can }
+    }
+
+    eval { Qux->foo() };
+    like($@, qr{Can't call next::method/next::can/maybe::next::method on undefined package},
+         "->next::can on non-existing package name dies correctly")
+
 }
-- 
1.5.3.8




-----------------------------------------------------------------
---
Flags:
    category=core
    severity=low
---
Site configuration information for perl 5.11.0:

Configured by ilmari at Sat Jan 26 04:38:10 GMT 2008.

Summary of my perl5 (revision 5 version 11 subversion 0 patch 33054) configuration:
  Platform:
    osname=linux, osvers=2.6.24-4-generic, archname=i686-linux
    uname='linux vesla 2.6.24-4-generic #1 smp mon jan 14 17:30:39 utc 2008 i686 gnulinux '
    config_args='-Dusedevel -Duserelocatableinc -de -Dprefix=/home/ilmari/perl510'
    hint=recommended, useposix=true, d_sigaction=define
    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 -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O2',
    cppflags='-fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include'
    ccversion='', gccversion='4.2.3 20080114 (prerelease) (Ubuntu 4.2.2-7ubuntu1)', 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 =' -fstack-protector -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib /usr/lib64
    libs=-lnsl -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 -O2 -L/usr/local/lib'

Locally applied patches:
    DEVEL

---
@INC for perl 5.11.0:
    /home/ilmari/perl510/lib/5.11.0/i686-linux
    /home/ilmari/perl510/lib/5.11.0
    /home/ilmari/perl510/lib/site_perl/5.11.0/i686-linux
    /home/ilmari/perl510/lib/site_perl/5.11.0
    .

---
Environment for perl 5.11.0:
    HOME=/home/ilmari
    LANG=en_GB.UTF-8
    LANGUAGE (unset)
    LC_TIME=en_IE.UTF-8
    LD_LIBRARY_PATH=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/lib
    LOGDIR (unset)
    PATH=/home/ilmari/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games:/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin
    PERL_BADLANG (unset)
    SHELL=/bin/bash


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