From 6f369dfc3d4d06cbf0c4faa74f8876b7f4a90f83 Mon Sep 17 00:00:00 2001
From: Steve Hancock <perltidy@users.sourceforge.net>
Date: Fri, 4 Sep 2020 20:59:23 -0700
Subject: [PATCH] add extra safety check for the -scbb -csc problem

---
 lib/Perl/Tidy/VerticalAligner.pm | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/lib/Perl/Tidy/VerticalAligner.pm b/lib/Perl/Tidy/VerticalAligner.pm
index 7a87109f..7c799d9e 100644
--- a/lib/Perl/Tidy/VerticalAligner.pm
+++ b/lib/Perl/Tidy/VerticalAligner.pm
@@ -4137,6 +4137,18 @@ sub get_output_line_number {
         my $line        = $leading_string . $str;
         my $line_length = $leading_string_length + $str_length;
 
+	# Safety check: be sure that a line to be cached as a stacked block
+	# brace line ends in the appropriate opening or closing block brace.
+	# This should always be the case if the caller set flags correctly.
+	# Code '3' is for -sobb, code '4' is for -scbb.
+        if ($open_or_close) {
+            if (   $open_or_close == 3 && $line !~ /\{\s*$/
+                || $open_or_close == 4 && $line !~ /\}\s*$/ )
+            {
+                $open_or_close = 0;
+            }
+        } 
+
         # write or cache this line
         if ( !$open_or_close || $side_comment_length > 0 ) {
             $self->valign_output_step_C( $line, $leading_space_count, $level );
-- 
2.39.5