Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to fix gsub with zero length matches #2564

Closed
wants to merge 1 commit into from

Conversation

weeble
Copy link

@weeble weeble commented Mar 30, 2023

Problem: If a regex given to gsub can match a zero length string, it will never terminate and eventually crash. This is because after matching once it keeps checking for the same match at the same location and finding it.

Solution: We can't skip ahead because there might be a non-zero length match at the same location. Instead, keep track of whether the last match was zero length, and when it was, ignore any zero length match at the same offset.

Remaining problem: This fails one of the new tests I added. For some reason, match doesn't seem to correctly return (some) zero length matches at the end of the input string. I'm going to report that as a separate bug, I can't see why it behaves like that.

Ref: #2148

Problem: If a regex given to gsub can match a zero length string, it
will never terminate and eventually crash. This is because after
matching once it keeps checking for the same match at the same location
and finding it.

Solution: We can't skip ahead because there might be a non-zero length
match at the same location. Instead, keep track of whether the last
match was zero length, and when it was, ignore any zero length match at
the same offset.

Remaining problem: This fails one of the new tests I added. For some
reason, match doesn't seem to correctly return (some) zero length
matches at the end of the input string. I'm going to report that as a
separate bug, I can't see why it behaves like that.

Ref: jqlang#2148
@wader
Copy link
Member

wader commented Mar 31, 2023

Hi, thanks for look into this. jq's maintenance is a bit in limbo at the moment, see #2305

@weeble
Copy link
Author

weeble commented Mar 31, 2023

Thanks for the heads-up!

@itchyny itchyny added the bug label Jun 3, 2023
@itchyny itchyny modified the milestone: 1.7 release Jul 9, 2023
@itchyny
Copy link
Contributor

itchyny commented Jul 9, 2023

The untermination issue of gsub with empty matches has been fixed by #2641.

@itchyny itchyny closed this Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants