about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2024-02-13 09:42:41 +0000
committerEric Wong <e@80x24.org>2024-02-14 07:26:10 +0000
commiteb2ce64095383b3ecf9edc492f908bfce3a88beb (patch)
tree429d1266dea3132c9009a2b9d11dc7d956585cc8
parent8f0c9c28121daad5b9441e51aa19e42d67b0e08b (diff)
downloadpublic-inbox-eb2ce64095383b3ecf9edc492f908bfce3a88beb.tar.gz
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.
-rw-r--r--lib/PublicInbox/Eml.pm18
1 files 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;
 }