dotnet / dotnet/wpf

[TextBoxView] Crash during large selection while background layout is incomplete

Open
#11,460 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

# [TextBoxView] Crash during large selection while background layout is incomplete

## Summary
`TextBoxView.GetLineIndexFromOffset` can fail during large-text selection when layout is still in time-sliced background progress, causing a crash/fail-fast path instead of graceful handling.

## Observed behavior
- Selecting text in very large `TextBox` content (for example, very long lines / high offsets) can trigger a consistency/assert failure.
- The failure happens when selection/highlight processing requests an offset beyond the currently measured line range.

## Repro notes (public)
1. Create/open a WPF app view with a `TextBox` containing a very large amount of text (for example, one very long line or total offsets in 100K+ range).
2. Trigger selection operations while layout is still being processed incrementally (IME/search/focus interactions can increase likelihood).
3. Observe crash/fail-fast behavior from `TextBoxView.GetLineIndexFromOffset` path.

## Impact
- Affects reliability of text editing/selection in large-document scenarios.
- More likely in interactive flows where layout and selection/focus updates interleave.

## Expected behavior
Selection/highlight should remain stable and never crash, even when background layout has not measured all lines yet.

## Actual behavior
The lookup assumes the requested offset is always inside measured `_lineMetrics`, and can fail when it is temporarily outside that range.

## Suspected root cause
`TextBoxView` performs time-sliced layout, so `_lineMetrics` may be only partially populated. During that window, selection/highlight can request an offset greater than the last measured line `EndOffset`, and the search path fails.

## Proposed fix direction
- Add defensive clamping in `GetLineIndexFromOffset` for offsets beyond currently measured range.
- Return the last measured line index when background layout is pending.
- Keep correctness checks for unexpected conditions while avoiding crash in expected incremental-layout windows.
- Remove redundant preconditions that incorrectly assume full containment during transitional layout states.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.