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

[PATCH] dodge a valgrind error (for maint or blead)

Thread Next
From:
Jarkko Hietaniemi
Date:
February 22, 2006 11:16
Subject:
[PATCH] dodge a valgrind error (for maint or blead)
Message ID:
43FCB896.7060106@gmail.com
Back in November timeframe I used to harp about this

$_ = 'a';
s/a//e;
print eval '$&';

giving a burp in valgrind, e.g. (still) in Perl 5.8.8:

==6672== Invalid read of size 1
==6672==    at 0x1BA094DD: memmove (in /lib/libc-2.3.2.so)
==6672==    by 0x80BA7E9: Perl_sv_setpvn (in /tmp/jhi/perl-5.8.8/perl)
==6672==    by 0x80A4BF0: Perl_magic_get (in /tmp/jhi/perl-5.8.8/perl)
==6672==    by 0x80A418B: Perl_mg_get (in /tmp/jhi/perl-5.8.8/perl)
==6672==    by 0x80BA64B: Perl_sv_setsv_flags (in /tmp/jhi/perl-5.8.8/perl)
==6672==    by 0x80BE780: Perl_sv_mortalcopy (in /tmp/jhi/perl-5.8.8/perl)
==6672==    by 0x80DA59C: Perl_pp_leaveeval (in /tmp/jhi/perl-5.8.8/perl)
==6672==    by 0x80AE4D7: Perl_runops_standard (in /tmp/jhi/perl-5.8.8/perl)
==6672==    by 0x8061663: S_run_body (in /tmp/jhi/perl-5.8.8/perl)
==6672==    by 0x80615E5: perl_run (in /tmp/jhi/perl-5.8.8/perl)
==6672==    by 0x805E83C: main (in /tmp/jhi/perl-5.8.8/perl)
==6672==  Address 0x1BAE9DB0 is 0 bytes inside a block of size 4 free'd
==6672==    at 0x1B904B04: free (vg_replace_malloc.c:152)
==6672==    by 0x80D2247: Perl_pp_substcont (in /tmp/jhi/perl-5.8.8/perl)
==6672==    by 0x80AE4D7: Perl_runops_standard (in /tmp/jhi/perl-5.8.8/perl)
==6672==    by 0x8061663: S_run_body (in /tmp/jhi/perl-5.8.8/perl)
==6672==    by 0x80615E5: perl_run (in /tmp/jhi/perl-5.8.8/perl)
==6672==    by 0x805E83C: main (in /tmp/jhi/perl-5.8.8/perl)

I tried to analyse this a bit and the best I could come up with is
that the whole PL_sawampersand logic (trying to avoid copying the
matched string) seems dubious at best to me.  I still haven't got
a sound story on what goes wrong exactly where (you can find my
monologues on the subject by searching for "valgrind" in any p5p
archives and looking for last November).

But now I have a rather well-contained patch to suggest, that doesn't
seem to affect at least "make test" run times too much (within
statistical noise): basically the patch simply turns on "save the
matched string" also when /e is present in s///.

I tried thinking of simple tests that would demonstrate the
copying/not-copying speed difference with/without the patch,
if any, but couldn't find any test that would have shown significant
differences with the patch (attached below) or without.  Maybe someone
else can think of such tests?  (I, of course, hope no such tests can be
devised so that we can get rid of the premature free that valgrind found
:-)




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