From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.1 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 3EC671F461 for ; Tue, 13 Feb 2024 09:42:43 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 1/2] eml: avoid anonymous __WARN__ sub for encode/decode Date: Tue, 13 Feb 2024 09:42:41 +0000 Message-Id: <20240213094242.3496966-2-e@80x24.org> In-Reply-To: <20240213094242.3496966-1-e@80x24.org> References: <20240213094242.3496966-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Repeatedly allocating an anonymous sub is an expensive operation and a potential source of leaks in older Perl. Instead, `local'-ize a global and use a permanent sub to workaround the old Encode 2.87..3.12 leak. --- lib/PublicInbox/Eml.pm | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/PublicInbox/Eml.pm b/lib/PublicInbox/Eml.pm index 24060ec8..56aec1e5 100644 --- a/lib/PublicInbox/Eml.pm +++ b/lib/PublicInbox/Eml.pm @@ -334,6 +334,11 @@ sub body_set { undef; } +# workaround https://rt.cpan.org/Public/Bug/Display.html?id=139622 +# Encode 2.87..3.12 leaks on croak, so we defer and croak ourselves +our @enc_warn; +my $enc_warn = sub { push @enc_warn, @_ }; + sub body_str_set { my ($self, $str) = @_; my $cs = ct($self)->{attributes}->{charset} // @@ -341,10 +346,10 @@ sub body_str_set { my $enc = find_encoding($cs) // croak "unknown encoding `$cs'"; my $tmp; { - my @w; - local $SIG{__WARN__} = sub { push @w, @_ }; + local @enc_warn; + local $SIG{__WARN__} = $enc_warn; $tmp = $enc->encode($str, Encode::FB_WARN); - croak(@w) if @w; + croak(@enc_warn) if @enc_warn; }; body_set($self, \$tmp); } @@ -471,11 +476,10 @@ sub body_str { }; my $enc = find_encoding($cs) or croak "unknown encoding `$cs'"; my $tmp = body($self); - # workaround https://rt.cpan.org/Public/Bug/Display.html?id=139622 - my @w; - local $SIG{__WARN__} = sub { push @w, @_ }; + local @enc_warn; + local $SIG{__WARN__} = $enc_warn; my $ret = $enc->decode($tmp, Encode::FB_WARN); - croak(@w) if @w; + croak(@enc_warn) if @enc_warn; $ret; }