From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.0 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 8ED0B1F855 for ; Fri, 5 Aug 2016 01:03:06 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH] thread: avoid recursion in Mail::Thread::recurse_down Date: Fri, 5 Aug 2016 01:03:06 +0000 Message-Id: <20160805010306.7552-1-e@80x24.org> List-Id: Yet another monkey patch to fix a problem encountered in upstream Mail::Thread. ref: - https://rt.cpan.org/Ticket/Display.html?id=116727 - http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=833479 --- lib/PublicInbox/Thread.pm | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/PublicInbox/Thread.pm b/lib/PublicInbox/Thread.pm index 44a565a..8af9461 100644 --- a/lib/PublicInbox/Thread.pm +++ b/lib/PublicInbox/Thread.pm @@ -5,6 +5,10 @@ # - http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=795913 # - https://rt.cpan.org/Ticket/Display.html?id=106498 # +# And avoid recursion in recurse_down: +# - https://rt.cpan.org/Ticket/Display.html?id=116727 +# - http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=833479 +# # License differs from the rest of public-inbox (but is compatible): # This library is free software; you can redistribute it and/or modify # it under the same terms as Perl itself. @@ -42,6 +46,32 @@ sub topmost { $_[0]->SUPER::topmost || PublicInbox::Thread::CPANRTBug106498->new; } +# non-recursive version of recurse_down to avoid stack depth warnings +sub recurse_down { + my ($self, $callback) = @_; + my %seen; + my @q = ($self); + while (my $cont = shift @q) { + $seen{$cont}++; + $callback->($cont); + + if (my $next = $cont->next) { + if ($seen{$next}) { + $cont->next(undef); + } else { + push @q, $next; + } + } + if (my $child = $cont->child) { + if ($seen{$child}) { + $cont->child(undef); + } else { + push @q, $child; + } + } + } +} + # ref: # - http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=795913 # - https://rt.cpan.org/Ticket/Display.html?id=106498 -- EW