]> Sergey Matveev's repositories - public-inbox.git/commitdiff
thread: avoid recursion in Mail::Thread::recurse_down
authorEric Wong <e@80x24.org>
Fri, 5 Aug 2016 00:24:18 +0000 (00:24 +0000)
committerEric Wong <e@80x24.org>
Fri, 5 Aug 2016 00:58:36 +0000 (00:58 +0000)
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

index 44a565ad3c7e267196e086ceeb3e00be174a3197..8af94616f73d85edeaa50496dccb990c8c511906 100644 (file)
@@ -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