[Conhost] Fix off-by-1 errors for search and color selection foreground - #20519
[Conhost] Fix off-by-1 errors for search and color selection foreground#20519Carlos Zamora (carlos-zamora) wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Investigate.
srSelectionRect.rightis already inclusive --> right side of the character- CopyRequest::end expects exclusive
Why do we need to add 1, if we're on the right side of the character already.
| // beg and end coordinates are inclusive | ||
| til::point beg; | ||
| til::point end; | ||
| til::point beg; // inclusive | ||
| til::point end; // exclusive |
There was a problem hiding this comment.
Investigate.
- when did this occur? How long has this been a problem?
Carlos Zamora (carlos-zamora)
left a comment
There was a problem hiding this comment.
TODO: Have GHCP do a full audit of logic and comments
Resulting report using GHCP - Opus 5 - 1M context - Max reasoningAudit: the #18106 "exclusive selection range" effortRepository: Scope & methodTraced the full change surface of #18106 (
Layers reviewed: Findings A, B, and C were empirically proven by building
🔴 The root cause — fix this first
// Searches through the entire (committed) text buffer for `needle` ...
// The end coordinates of the returned ranges are considered inclusive. // ← WRONG
std::optional<std::vector<til::point_span>> TextBuffer::SearchText(...) const
// Searches through the given rows [rowBeg,rowEnd) for `needle` ...
// While the end coordinates of the returned ranges are considered inclusive, // ← WRONG
// the [rowBeg,rowEnd) range is half-open.#18106 changed the underlying - // Returns an inclusive point range given a text start and end position.
+ // Returns a half-open [beg,end) range given a text start and end position.
...
- auto nativeIndexEnd = uregex_end64(re, 0, &status);
- // The parameters are given as a half-open [beg,end) range, but the point_span we return in closed [beg,end].
- nativeIndexEnd--;
+ const auto nativeIndexEnd = uregex_end64(re, 0, &status);
...
- ret.end.x = ...GetTrailingColumnAtCharOffset(ut->chunkOffset);
+ ret.end.x = ...GetLeadingColumnAtCharOffset(ut->chunkOffset);…but the public Fix: change both comments to "half-open Real bugsA.
|

Summary of the Pull Request
Fixes a number of off-by-one errors in conhost. Specifically, the issues were with search, color selection (foreground), and the UIA find text API.
To minimize risk and make a small, concentrated change, I tried making targeted fixes with concise comments explaining why the change is needed. An earlier approach was to be more explicit about inclusive/exclusive coordinates using strict typing, but that seemed more harmful than helpful.
References and Relevant Issues
#18106
Validation Steps Performed
echo foo foo fooreg add "HKCU\Console" /v EnableColorSelection /t REG_DWORD /d 1 /fecho foo foo fooPR Checklist
TestColorSelectionSearchAndColorAllMatches)