BenV's notes

Tag: libxml

How to segfault perl

by on Mar.06, 2011, under Fun

Yesterday when I was messing around with our homebrew IRC bot (written from scratch in perl) it somehow managed to segfault.
Which surprised me, it doesn’t happen very often that I see perl segfault.
It was caused by the fairly new module that handles RSS feeds. Running it through gdb quickly pointed a finger at XML::LibXML, which probably surprises nobody 😉
(at least I suspected the moment it crashed)
It looked like LibXML tried to free up some resource that didn’t exist anymore.

Today I wrote a little test script to figure out the exact cause. Apparently, using Storable to store things that LibXML returned is a recipe for great fun 🙂
Actually, storing it works pretty good. It works so damn good that LibXML actually believes that the objects loaded through Storable are still valid.

Example.
First store a piece of XML::LibXML to disk:

#!/usr/bin/perl
use Data::Dumper;
use Storable qw/lock_nstore/;
use XML::LibXML;

die "Usage: $0 file.storable" unless(scalar(@ARGV));

my $rssfeed = 'Dummy RSS feed';
my $data = {};

my $xmlp = XML::LibXML->new();
my $xml = $xmlp->parse_string($rssfeed);
$data->{'xml'} = $xml->find('/rss/channel/title/text()');
my $s = lock_nstore($data, $ARGV[0]);
print "Stored.\n";


#!/usr/bin/perl
use Storable qw/retrieve/;
use XML::LibXML;

die "Usage: $0 file.storable" unless(scalar(@ARGV));
my $s = retrieve($ARGV[0]);
my $link = $s->{'xml'};
print "Segfault in progress! $link\n";

The execution looks like this:

benv@janeman:~:0>./test-store.pl asd
Stored.
benv@janeman:~:0>./test-load.pl asd
Segmentation fault (core dumped)

Fun!

Leave a Comment :, , , , more...