Skip to content

check buffer size before reading BOM in setupInputStream - #235

Open
zayeem06 wants to merge 1 commit into
antlr:masterfrom
zayeem06:bom-bounds-check
Open

check buffer size before reading BOM in setupInputStream#235
zayeem06 wants to merge 1 commit into
antlr:masterfrom
zayeem06:bom-bounds-check

Conversation

@zayeem06

Copy link
Copy Markdown

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.

@zayeem06

Copy link
Copy Markdown
Author

any update?

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