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

StackOverflowError for Machine.add #77

Open
paplorinc opened this issue Oct 6, 2018 · 2 comments
Open

StackOverflowError for Machine.add #77

paplorinc opened this issue Oct 6, 2018 · 2 comments

Comments

@paplorinc
Copy link

Hey, I was combining many (>10.000) similar file names to a unified regexp (all escaped and |-ed).
In Java it compiles (though is extremely slow), but in Re2/j it fails with a stack overflow:

Caused by: java.lang.StackOverflowError
	at com.google.re2j.Machine.add(Machine.java:358)
	at com.google.re2j.Machine.add(Machine.java:358)
etc ...
@sjamesr
Copy link
Contributor

sjamesr commented Oct 6, 2018

Are you able to share a code snippet demonstrating the problem?

Also, maybe for your use case it's better to add all the strings to a HashSet and use contains.

@michaelbraun
Copy link

michaelbraun commented Mar 16, 2021

We hit this as well - this Scala snippet will hit the problem:

import com.google.re2j.Pattern

object TestRE2JStackOverflow {

  private val limit = 10000
  private val charRange = Range('a', 'z')

  def main(args: Array[String]): Unit = {
    val patterns =
      for (charOne <- charRange;
        charTwo <- charRange;
        charThree <- charRange;
        charFour <- charRange) yield {

        val codepoints = Array[Int](charOne, charTwo, charThree, charFour)
        val newString = new String(codepoints, 0, 4)

        val stringToUse = ".*" + newString + "$"

        stringToUse
      }

    val combinedPatternString = patterns.slice(0, limit).mkString("(?:", "|", ")")

    val p: Pattern = Pattern.compile(combinedPatternString)

    p.matches(" one two three four test ")
  }

And the stack overflow:

	at com.google.re2j.Machine.add(Machine.java:380)
	at com.google.re2j.Machine.add(Machine.java:380)
	at com.google.re2j.Machine.add(Machine.java:380)
	at com.google.re2j.Machine.add(Machine.java:380)
	at com.google.re2j.Machine.add(Machine.java:380)

Which originates from:

	at com.google.re2j.Machine.add(Machine.java:380)
	at com.google.re2j.Machine.match(Machine.java:253)
	at com.google.re2j.RE2.doExecute(RE2.java:246)
	at com.google.re2j.RE2.match(RE2.java:283)
	at com.google.re2j.Matcher.genMatch(Matcher.java:327)
	at com.google.re2j.Matcher.find(Matcher.java:304)

This is RE2J 1.4

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

No branches or pull requests

3 participants