Fix crashes in BSLineNumberRulerView when the lineIndex_ is empty.
authorRobert Sesek <rsesek@bluestatic.org>
Sun, 8 Dec 2019 18:42:32 +0000 (13:42 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Sun, 15 Dec 2019 17:08:04 +0000 (12:08 -0500)
This may happen if file access is not granted and source files cannot be
read.

Source/BSLineNumberRulerView.mm

index b530bf04143f719630c4bf930310750d6240c732..f40e94b17802ad9846d016b1942a8e8eeafa9604 100644 (file)
@@ -142,10 +142,14 @@ const CGFloat kRulerRightPadding = 2.5;
   [self computeLineIndex];
 
   // Determine the width of the ruler based on the line count.
-  NSUInteger lastElement = lineIndex_.back() + 1;
-  NSAttributedString* lastElementString = [self attributedStringForLineNumber:lastElement];
-  NSSize boundingSize = [lastElementString size];
-  [self setRuleThickness:std::max(kDefaultWidth, boundingSize.width)];
+  if (lineIndex_.empty()) {
+    [self setRuleThickness:kDefaultWidth];
+  } else {
+    NSUInteger lastElement = lineIndex_.back() + 1;
+    NSAttributedString* lastElementString = [self attributedStringForLineNumber:lastElement];
+    NSSize boundingSize = [lastElementString size];
+    [self setRuleThickness:std::max(kDefaultWidth, boundingSize.width)];
+  }
 
   [self setNeedsDisplay:YES];
 }
@@ -209,6 +213,9 @@ const CGFloat kRulerRightPadding = 2.5;
     index = NSMaxRange([text lineRangeForRange:NSMakeRange(index, 0)]);
   }
 
+  if (lineIndex_.empty())
+    return;
+
   NSUInteger lineEnd, contentEnd;
   [text getLineStart:NULL
                  end:&lineEnd