Backtestsjsouprun-20261006-181938#2548
Normalize re2j exceptions
The merged fix was judged better than our patch.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the root cause the issue describes. Before either change, only re2j compile was guarded; a StackOverflowError raised during re2j matching (Matcher.find, which is where re2j#77's recursion occurs) escaped as a raw Error. Both patches wrap Re2jRegex's find() and rethrow a ValidationException, so on correctness for the reported re2j case they are equivalent.
Patch A also adds the same handling to the JDK regex path, in both compile and find. That is extra hardening beyond the issue's re2j ask, so it earns no credit on its own. It is mostly harmless, but it widens the behavior change to every jsoup user, not just re2j users.
On craft, Patch A keeps the `"Pattern complexity error: " + e.getMessage()` form. StackOverflowError usually has a null message, so users will typically see "Pattern complexity error: null". Patch B emits a clean "Pattern complexity error" message via a named constant, which matches the issue's wording exactly.
On tests, Patch B adds a focused regression test that reproduces the real-world scenario: a selector query (`[data-key~=(a|){824}]`) run through doc.select with re2j enabled. It asserts the exact normalized message, and it would fail before the change with a raw StackOverflowError. Patch A's tests are broader but weaker in two ways. Its re2j compile test exercises a path that was already guarded before the patch, so it would pass without the fix. Its other tests depend on 100k-deep inputs and default stack sizes, which makes them heavier and potentially environment-sensitive, though its re2j find test is a valid regression test.
Overall, B delivers the issue's fix more cleanly and precisely, with a tighter targeted test. A adds broader but unrequested scope and produces a sloppy 'null'-suffixed message.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
13m 4s
Wall-clock time in the container
Full test suite
No new failures
2,095 passed · 0 skipped · 2,095 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
99vs 23 merged
Changed lines, ours vs merged
The issue
View on GitHubre2j can throw a Stack Overflow if the input regex causes a deep recursion. We should normalize that into an Pattern complexity error ValidationException (which is an IllegalArgumentException) so it's clearer what the issue is.
Underlying root cause is https://github.com/google/re2j/issues/77
The two patches
CHANGES.mdindex fdd6a17a..b5862685 100644@@ -19,6 +19,7 @@ * Preserve control characters in parsed tag names [#2358](https://github.com/jhy/jsoup/issues/2538) * Updated HTTP redirects to follow the specification: 307 and 308 preserve the request method and content, 301 and 302 only change POST to GET, and `Location` is followed only for 301, 302, 303, 307, and 308 responses. Streamed request bodies are not buffered; if an automatic redirect would require replaying one, execution fails so the caller can resend with a fresh stream. [#2540](https://github.com/jhy/jsoup/pull/2540) * Corrected the Cleaner's same-site link detection to compare hostnames rather than URL prefixes when applying `rel=nofollow`. [2543](https://github.com/jhy/jsoup/issues/2543)+* Regular expressions that are too complex for the regex engine to compile or evaluate (e.g. deeply nested groups that cause re2j to overflow the stack, per [re2j#77](https://github.com/google/re2j/issues/77)) now throw a `ValidationException` (an `IllegalArgumentException`) with a `Pattern complexity error` message, instead of a raw `StackOverflowError`. This applies both when compiling and when matching, with re2j or the JDK regex engine. ### Build Changes * Cleaned up the Maven build for the multi-release JAR so Java 8 and Java 11+ sources compile as separate source sets. This avoids spurious Java 8 compiler warnings from newer-language overlay sources, keeps long-running parser checks behind an explicit profile, and preserves the same published artifacts and runtime behavior.src/main/java/org/jsoup/helper/Re2jRegex.javaindex b9e65bed..6fcf3020 100644@@ -19,7 +19,7 @@ final class Re2jRegex extends Regex { } catch (RuntimeException e) { throw new ValidationException("Pattern syntax error: " + e.getMessage()); } catch (OutOfMemoryError | StackOverflowError e) { // defensive check on regex to normalize exception- throw new ValidationException("Pattern complexity error: " + e.getMessage());+ throw complexityError(e); } } @@ -42,7 +42,11 @@ final class Re2jRegex extends Regex { @Override public boolean find() {- return delegate.find();+ try {+ return delegate.find();+ } catch (StackOverflowError e) { // re2j recurses when following empty-width instructions; https://github.com/google/re2j/issues/77+ throw complexityError(e);+ } } } }src/main/java/org/jsoup/helper/Regex.javaindex 7b157ddc..72066978 100644@@ -30,7 +30,7 @@ public class Regex { @param regex the regex to compile @return the compiled regex- @throws ValidationException if the regex is invalid+ @throws ValidationException if the regex is invalid, or too complex to compile */ public static Regex compile(String regex) { if (usingRe2j()) {@@ -41,9 +41,19 @@ public class Regex { return new Regex(Pattern.compile(regex)); } catch (PatternSyntaxException e) { throw new ValidationException("Pattern syntax error: " + e.getMessage());+ } catch (OutOfMemoryError | StackOverflowError e) { // defensive check on regex to normalize exception+ throw complexityError(e); } } + /**+ Normalizes a resource exhaustion error raised by a regex engine (e.g. a StackOverflowError from deep recursion when+ compiling or matching a complex pattern) into a ValidationException.+ */+ static ValidationException complexityError(Throwable e) {+ return new ValidationException("Pattern complexity error: " + e.getMessage());+ }+ /** Wraps an existing JDK Pattern (for API compat); doesn't switch */ public static Regex fromPattern(Pattern pattern) { return new Regex(pattern);@@ -101,6 +111,11 @@ public class Regex { } public interface Matcher {+ /**+ Attempts to find the next subsequence of the input that matches the pattern.+ @return true if a match was found+ @throws ValidationException if the pattern is too complex to evaluate against the input+ */ boolean find(); } @@ -113,7 +128,11 @@ public class Regex { @Override public boolean find() {- return delegate.find();+ try {+ return delegate.find();+ } catch (StackOverflowError e) { // backtracking can recurse deeply on complex patterns / long inputs+ throw complexityError(e);+ } } } }src/test/java/org/jsoup/helper/RegexTest.javaindex 14398df0..5062c79e 100644@@ -1,5 +1,7 @@ package org.jsoup.helper; +import org.jsoup.Jsoup;+import org.jsoup.nodes.Document; import org.jsoup.select.QueryParser; import org.jsoup.select.Selector; import org.junit.jupiter.api.AfterEach;@@ -72,4 +74,69 @@ public class RegexTest { } assertTrue(threw); }++ private static String repeat(String s, int count) {+ StringBuilder sb = new StringBuilder(s.length() * count);+ for (int i = 0; i < count; i++) sb.append(s);+ return sb.toString();+ }++ @Test+ void re2jDeeplyNestedPatternCompileThrowsComplexityError() {+ // re2j recurses in its parser on nested groups, and overflows the stack: https://github.com/google/re2j/issues/77+ Regex.wantsRe2j(true);+ assertTrue(Regex.usingRe2j());+ int depth = 100_000;+ String pattern = repeat("(", depth) + "a" + repeat(")", depth);++ ValidationException e = assertThrows(ValidationException.class, () -> Regex.compile(pattern));+ assertTrue(e.getMessage().startsWith("Pattern complexity error"), e.getMessage());++ // and via the Element API (a selector query of this length can't be tokenized, so use the regex methods directly)+ Document doc = Jsoup.parse("<p title=a>a</p>");+ ValidationException ee = assertThrows(ValidationException.class,+ () -> doc.getElementsByAttributeValueMatching("title", pattern));+ assertTrue(ee.getMessage().startsWith("Pattern complexity error"), ee.getMessage());+ }++ @Test+ void re2jComplexPatternFindThrowsComplexityError() {+ // compiles OK, but re2j recurses through the long chain of empty-width instructions when matching+ Regex.wantsRe2j(true);+ assertTrue(Regex.usingRe2j());+ int count = 100_000;+ String pattern = repeat("a?", count);+ Regex regex = Regex.compile(pattern);+ Regex.Matcher matcher = regex.matcher(repeat("a", count));++ ValidationException e = assertThrows(ValidationException.class, matcher::find);+ assertTrue(e.getMessage().startsWith("Pattern complexity error"), e.getMessage());++ // and when evaluating against a document+ Document doc = Jsoup.parse("<p>" + repeat("a", count) + "</p>");+ ValidationException se = assertThrows(ValidationException.class,+ () -> doc.getElementsMatchingText(pattern));+ assertTrue(se.getMessage().startsWith("Pattern complexity error"), se.getMessage());+ ValidationException ee = assertThrows(ValidationException.class,+ () -> doc.getElementsMatchingOwnText(pattern));+ assertTrue(ee.getMessage().startsWith("Pattern complexity error"), ee.getMessage());+ }++ @Test+ void jdkBacktrackingFindThrowsComplexityError() {+ // the JDK engine recurses per repetition of a group alternation, so overflows on long inputs+ Regex.wantsRe2j(false);+ int count = 100_000;+ String pattern = "(a|b)*";+ Regex regex = Regex.compile(pattern);+ Regex.Matcher matcher = regex.matcher(repeat("a", count));++ ValidationException e = assertThrows(ValidationException.class, matcher::find);+ assertTrue(e.getMessage().startsWith("Pattern complexity error"), e.getMessage());++ Document doc = Jsoup.parse("<p>" + repeat("a", count) + "</p>");+ ValidationException se = assertThrows(ValidationException.class,+ () -> doc.select(":matchesOwn(" + pattern + ")"));+ assertTrue(se.getMessage().startsWith("Pattern complexity error"), se.getMessage());+ } }
CHANGES.mdindex fdd6a17a..de6ee016 100644@@ -12,6 +12,7 @@ * Improved redirect credential handling as a defense-in-depth measure: explicit authorization headers and request cookies are no longer forwarded across origins, reducing exposure through open redirects and aligning with HTTP guidance. Cookies managed by a `CookieStore` continue to follow their configured scope. [#2540](https://github.com/jhy/jsoup/pull/2540) * Elements can now append their outer HTML, including their own tags, directly to an `Appendable` with `Node#outerHtml(Appendable)`, without first creating a `String`. This complements `Element#html(Appendable)`, which appends inner HTML only. [#2532](https://github.com/jhy/jsoup/issues/2532) * Aligned CDATA tokenization with the HTML spec: CDATA syntax in HTML content is parsed as a bogus comment, while it remains supported in SVG, MathML, and XML. Also improved namespace-aware fragment parsing so SVG and MathML contexts, HTML integration points, and context-sensitive tokenizer states are handled correctly. [#2542](https://github.com/jhy/jsoup/issues/2542)+* When using the optional `re2j` regular expression engine, stack overflows caused by complex selector patterns are now normalized to a `ValidationException` with a `Pattern complexity error` message. [#2548](https://github.com/jhy/jsoup/issues/2548) ### Bug Fixes * Fixed HTML parsing of mixed-case RCDATA end tags after tag-shaped text. For example, `<title><p>Foo</TiTLE>` and `<textarea><img src=x></TeXtArEa>` now keep the tag-shaped content as text instead of promoting it to markup. [#2503](https://github.com/jhy/jsoup/issues/2503)src/main/java/org/jsoup/helper/Re2jRegex.javaindex b9e65bed..478eb467 100644@@ -5,6 +5,7 @@ package org.jsoup.helper; */ final class Re2jRegex extends Regex { private static final java.util.regex.Pattern unused = java.util.regex.Pattern.compile("");+ private static final String PatternComplexityError = "Pattern complexity error"; private final com.google.re2j.Pattern re2jPattern; @@ -19,7 +20,7 @@ final class Re2jRegex extends Regex { } catch (RuntimeException e) { throw new ValidationException("Pattern syntax error: " + e.getMessage()); } catch (OutOfMemoryError | StackOverflowError e) { // defensive check on regex to normalize exception- throw new ValidationException("Pattern complexity error: " + e.getMessage());+ throw new ValidationException(PatternComplexityError); } } @@ -42,7 +43,11 @@ final class Re2jRegex extends Regex { @Override public boolean find() {- return delegate.find();+ try {+ return delegate.find();+ } catch (StackOverflowError e) { // re2j may recurse deeply when evaluating complex patterns+ throw new ValidationException(PatternComplexityError);+ } } } }src/test/java/org/jsoup/helper/RegexTest.javaindex 14398df0..1bf61fd5 100644@@ -1,5 +1,7 @@ package org.jsoup.helper; +import org.jsoup.Jsoup;+import org.jsoup.nodes.Document; import org.jsoup.select.QueryParser; import org.jsoup.select.Selector; import org.junit.jupiter.api.AfterEach;@@ -57,6 +59,17 @@ public class RegexTest { // and not the rej2 PatternSyntaxException } + @Test+ void re2jSelectorMatchComplexityThrowsValidationException() {+ Regex.wantsRe2j(true);+ assertTrue(Regex.usingRe2j());++ Document doc = Jsoup.parse("<p data-key>");+ String query = "[data-key~=(a|){824}]"; // empty alternative and repeat create deep recursion in RE2J+ ValidationException exception = assertThrows(ValidationException.class, () -> doc.select(query));+ assertEquals("Pattern complexity error", exception.getMessage());+ }+ @ParameterizedTest @ValueSource(booleans = {false, true}) void queryParserThrowsSelectorExceptionOnMalformedRegex(boolean useRe2j) {