check buffer size before reading BOM in setupInputStream - #235
Open
zayeem06 wants to merge 1 commit into
Open
Conversation
Author
|
any update? |
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.
setupInputStream in runtime/C/src/antlr3filestream.c peeks at the first two to four bytes of the input to detect a UTF-8/UTF-16/UTF-32 byte order mark, but it dereferences nextChar[0..3] without ever consulting input->sizeBuf, so opening a file or string stream that is shorter than the BOM under one of those encodings reads past the end of the buffer. A 1-byte input whose first byte matches a BOM lead byte is enough to trigger a heap over-read under ASan. I added an input->sizeBuf length check in front of each BOM comparison, which is where the read happens and where every other reader in the runtime already bounds-checks against data + sizeBuf, so a short buffer just skips BOM detection. Worth verifying that valid BOMs still get consumed: a proper UTF-16 BOM still advances two bytes and a UTF-8 BOM three.