Skip to content

clamp startIndex in subString to avoid unsigned length underflow - #242

Open
zayeem06 wants to merge 1 commit into
antlr:masterfrom
zayeem06:substr-index-underflow
Open

clamp startIndex in subString to avoid unsigned length underflow#242
zayeem06 wants to merge 1 commit into
antlr:masterfrom
zayeem06:substr-index-underflow

Conversation

@zayeem06

Copy link
Copy Markdown

subString8 and subStringUTF16 only clamp the upper index and then compute the copy length as endIndex - startIndex in unsigned arithmetic, so a call where startIndex is past endIndex (for example subString(s, 6, 5)) wraps the length to roughly 0xFFFFFFFF. That value reaches newPtr8/newSize8 where size + 1 overflows to 0, so a minimal buffer is allocated and the following memmove copies about 4GB, which AddressSanitizer flags as a heap overflow at newPtr8. subString is a public string method that generated parsers and grammar actions use to slice token text with offsets derived from the input, so reversed offsets are reachable. The fix clamps startIndex to endIndex in both functions so the subtraction can no longer underflow. Reviewers may want to confirm that ordinary substrings are untouched, which they are since the guard only fires when startIndex already exceeds endIndex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant