user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] eml: fix leak workaround
Date: Sat, 16 Oct 2021 23:23:01 +0000	[thread overview]
Message-ID: <20211016232301.20463-1-e@80x24.org> (raw)

Our previous workaround didn't actually work around the leak in
<https://rt.cpan.org/Public/Bug/Display.html?id=139622> since
croak()-via-Perl was still invoked before the SV reference
count could be decremented.

Put in a proper workaround which saves warnings onto a temporary
variable and only croak after ->decode or ->encode returns; not
inside those methods.
---
 lib/PublicInbox/Eml.pm | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/lib/PublicInbox/Eml.pm b/lib/PublicInbox/Eml.pm
index 69c26932..3c681ba5 100644
--- a/lib/PublicInbox/Eml.pm
+++ b/lib/PublicInbox/Eml.pm
@@ -333,15 +333,18 @@ sub body_set {
 }
 
 sub body_str_set {
-	my ($self, $body_str) = @_;
+	my ($self, $str) = @_;
 	my $cs = ct($self)->{attributes}->{charset} //
 		croak('body_str was given, but no charset is defined');
 	my $enc = find_encoding($cs) // croak "unknown encoding `$cs'";
-	$body_str = do {
-		local $SIG{__WARN__} = \&croak;
-		$enc->encode($body_str, Encode::FB_WARN);
+	my $tmp;
+	{
+		my @w;
+		local $SIG{__WARN__} = sub { push @w, @_ };
+		$tmp = $enc->encode($str, Encode::FB_WARN);
+		croak(@w) if @w;
 	};
-	body_set($self, \$body_str);
+	body_set($self, \$tmp);
 }
 
 sub content_type { scalar header($_[0], 'Content-Type') }
@@ -466,8 +469,11 @@ 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
-	local $SIG{__WARN__} = \&croak;
-	$enc->decode($tmp, Encode::FB_WARN);
+	my @w;
+	local $SIG{__WARN__} = sub { push @w, @_ };
+	my $ret = $enc->decode($tmp, Encode::FB_WARN);
+	croak(@w) if @w;
+	$ret;
 }
 
 sub as_string {

                 reply	other threads:[~2021-10-16 23:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211016232301.20463-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    --subject='Re: [PATCH] eml: fix leak workaround' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).