From 96abe13e3e2a331af0f796c11e8eeb85115444d2 Mon Sep 17 00:00:00 2001 From: Nicolas R Date: Mon, 24 Aug 2020 10:09:30 -0600 Subject: [PATCH] Avoid segfault when freeing parse RT-115034 Protect active parser from being freed URL: https://rt.cpan.org/Public/Bug/Display.html?id=115034 Author: sprout --- MANIFEST | 1 + Parser.xs | 1 + t/free.t | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 t/free.t diff --git a/MANIFEST b/MANIFEST index 98812cb..8e80608 100644 --- a/MANIFEST +++ b/MANIFEST @@ -43,6 +43,7 @@ t/entities.t Test encoding/decoding of entities t/entities2.t Test _decode_entities() t/filter-methods.t Test ignore_tags, ignore_elements methods. t/filter.t Test HTML::Filter +t/free.t Test freeing of active parser t/handler-eof.t Test invocation of $p->eof in handlers t/handler.t Test $p->handler method t/headparser-http.t Test HTML::HeadParser diff --git a/Parser.xs b/Parser.xs index e5afea8..2db2477 100644 --- a/Parser.xs +++ b/Parser.xs @@ -377,6 +377,7 @@ parse(self, chunk) PREINIT: PSTATE* p_state = get_pstate_hv(aTHX_ self); PPCODE: + (void)sv_2mortal(SvREFCNT_inc(SvRV(self))); if (p_state->parsing) croak("Parse loop not allowed"); p_state->parsing = 1; diff --git a/t/free.t b/t/free.t new file mode 100644 index 0000000..b462d5e --- /dev/null +++ b/t/free.t @@ -0,0 +1,19 @@ +#!perl + +use strict; +use warnings; + +use Test::More tests => 1; + +use HTML::Parser; + +my $p; +$p = HTML::Parser->new( + start_h => [sub { + undef $p; + }], +); + +$p->parse(q()); + +pass 'no SEGV'; \ No newline at end of file