Fix crash: clamp model offset in ctrlOffsetAfterMove + findBefore/findAfter#29
Merged
Conversation
…dAfter
Stack trace from DasherMac (macOS 26.5 hardened libc++):
string::operator[](this='aaaaaaaaaa.', __pos=75)
← findBefore(pos=75) at CAPI.cpp:89
← getRange(buf='aaaaaaaaaa.', ...) at CAPI.cpp:134
← ctrlOffsetAfterMove(offsetBefore=76) at CAPI.cpp:400
← DeleteAction::calculateNewOffset(offsetBefore=75) at ControlManager.cpp:193
← CContNode::PopulateChildren() at ControlManager.cpp:94
← CDasherModel::ExpandNode()
Root cause: ctrlOffsetAfterMove uses the model's node offset (75)
as an index into the edit buffer (11 chars). Model offset != edit
buffer length — they diverge because control nodes have offsets
in the model tree but produce no edit buffer characters.
Three fixes:
1. ctrlOffsetAfterMove: clamp offsetBefore to editBuffer.size()
before passing to getRange.
2. findBefore: clamp pos to s.size()-1 at entry. The function
accesses s[pos] and s[p] which crash if pos >= size.
3. findAfter: clamp pos to s.size() at entry. Same defensive
pattern for the forward search.
Signed-off-by: will wade <willwade@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Crash on DasherMac (macOS 26.5 hardened libc++):
string::operator[]assertion fires when expanding a control node that tries to calculate a delete offset.Stack trace
Root cause
ctrlOffsetAfterMoveuses the model's node offset (75) directly as an index into the edit buffer (11 chars). These diverge because control nodes have offsets in the model tree but produce no edit buffer characters — so the model offset can grow far beyond the buffer length.Fix
Three layers of defence:
ctrlOffsetAfterMove: clampoffsetBeforetoeditBuffer.size()before passing togetRangefindBefore: clamppostos.size()-1at entry (the function accessess[pos])findAfter: clamppostos.size()at entry (same defensive pattern)8 insertions, 1 deletion. No API changes.