From f593f6efa0e16fb5f307ec63402ac542a5de2fce Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 8 Dec 2019 13:42:32 -0500 Subject: [PATCH] Fix crashes in BSLineNumberRulerView when the lineIndex_ is empty. This may happen if file access is not granted and source files cannot be read. --- Source/BSLineNumberRulerView.mm | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Source/BSLineNumberRulerView.mm b/Source/BSLineNumberRulerView.mm index b530bf0..f40e94b 100644 --- a/Source/BSLineNumberRulerView.mm +++ b/Source/BSLineNumberRulerView.mm @@ -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 -- 2.43.5