From c654993b564b128b01cd57fbe6bf1a5aa753ee47 Mon Sep 17 00:00:00 2001 From: Zufu Liu Date: Tue, 17 Jun 2025 13:25:27 +1000 Subject: Feature [feature-requests:#1557]. Fix finding sub-line of position in bidirectional mode which is used for accessibility on macOS with VoiceOver. Avoid out-of-bounds access. --- src/PositionCache.cxx | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/PositionCache.cxx b/src/PositionCache.cxx index d0fb6acf7..16bc10f5a 100644 --- a/src/PositionCache.cxx +++ b/src/PositionCache.cxx @@ -167,22 +167,20 @@ bool LineLayout::InLine(int offset, int line) const noexcept { } int LineLayout::SubLineFromPosition(int posInLine, PointEnd pe) const noexcept { - if (!lineStarts || (posInLine > maxLineLength)) { + if (lines <= 1 || (posInLine >= numCharsBeforeEOL)) { return lines - 1; } - for (int line = 0; line < lines; line++) { - if (FlagSet(pe, PointEnd::subLineEnd)) { - // Return subline not start of next - if (lineStarts[line + 1] <= posInLine + 1) - return line; - } else { - if (lineStarts[line + 1] <= posInLine) - return line; + // Return subline not start of next for PointEnd::subLineEnd + posInLine -= FlagSet(pe, PointEnd::subLineEnd) ? 1 : 0; + int line = 1; + for (; line < lines; line++) { + if (lineStarts[line] > posInLine) { + break; } } - return lines - 1; + return line - 1; } void LineLayout::AddLineStart(Sci::Position start) { -- cgit v1.2.3