Skip to content

Conversation

Bertik23
Copy link
Contributor

@Bertik23 Bertik23 commented Aug 5, 2025

The IR Lexer has a getNextChar() function implemented. But it is used only once, on other places the pointer pointing to the current position in the lexed string (CurPtr) is incremented manually, this can lead to some OOB errors (see #151556 and Discourse thread).

To mitigate the issue, this PR changes the getNextChar() to be more straight forward, changes all manual pointer increments to getNextChar() or adds checks, so that no OOB occurs.

Two helper functions were used skipNChars and advancePositionTo. Where skipNChars(N) calls getNextChar() N-times, while advancePositionTo(Ptr) is used insead of CurPtr = Ptr.

One helper function isLabelTail was moved into the Lexer class, to be able to check the bounds, and renamed to getLabelTail to better reflect it's function.

@Bertik23
Copy link
Contributor Author

Bertik23 commented Aug 7, 2025

Ping @nikic as I haven't found a IR specific maintainer

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain the motivation for making everything go through getNextChar()? The discourse thread mentions that this is somehow LSP related, but it's not obvious to me how.

Using getNextChar() instead of incrementing CurPtr seems reasonable enough, I'm more curious about things like advancePositionTo which now do a byte-wise advance instead of the obvious thing.

@Bertik23
Copy link
Contributor Author

For the LSP, the lexer needs to know the current line:column location of the tokens. That's why everything goes through the getNextChar() as that would update the location and check for newlines, altho I don't think that in any of the current uses of advancePositionTo it is possible to get a on a new line, this would be more future proof if it would be used in such a case.

Co-authored-by: Nikita Popov <github@npopov.com>
@Bertik23
Copy link
Contributor Author

gentle ping @nikic

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.

2 participants