Skip to content

Fix IllegalArgumentException in LinkReferenceDefinitionParser. - #488

Open
cpovirk wants to merge 1 commit into
commonmark:mainfrom
cpovirk:iae
Open

cpovirk wants to merge 1 commit into
commonmark:mainfrom
cpovirk:iae

Conversation

@cpovirk

@cpovirk cpovirk commented Dec 4, 2026

Copy link
Copy Markdown

In 0982a26, LinkReferenceDefinitionParser started keeping committed link-reference source spans at the front of sourceSpans (0..referenceSourceSpanCount) until finishReference runs. When a block parser calls replaceActiveBlockParser or replaceParagraphLines with more lines than the uncommitted paragraph lines (such as Integer.MAX_VALUE), removeLines strips past referenceSourceSpanCount and finishReference throws IndexOutOfBoundsException.

To avoid this, clamp removeLines to the uncommitted trailing paragraph spans and clear that slice directly.

Here are the failures for the new tests before the prod change:

[ERROR] org.commonmark.test.BlockParserFactoryTest.replaceActiveBlockParserAfterLinkReferenceDefinitionWithUnclosedTitle -- Time elapsed: 0.002 s <<< ERROR!
java.lang.IndexOutOfBoundsException: toIndex = 1
        at java.base/java.util.AbstractList.subListRangeCheck(AbstractList.java:509)
        at java.base/java.util.ArrayList.subList(ArrayList.java:1199)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.LinkReferenceDefinitionParser.finishReference(LinkReferenceDefinitionParser.java:300)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.LinkReferenceDefinitionParser.getDefinitions(LinkReferenceDefinitionParser.java:110)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.ParagraphParser.getDefinitions(ParagraphParser.java:53)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.DocumentParser.addDefinitionsFrom(DocumentParser.java:595)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.DocumentParser.finalize(DocumentParser.java:590)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.DocumentParser.closeBlockParsers(DocumentParser.java:577)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.DocumentParser.replaceParagraphLines(DocumentParser.java:553)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.DocumentParser.parseLine(DocumentParser.java:297)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.DocumentParser.parse(DocumentParser.java:138)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.parser.Parser.parse(Parser.java:89)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.test.BlockParserFactoryTest.replaceActiveBlockParserAfterLinkReferenceDefinitionWithUnclosedTitle(BlockParserFactoryTest.java:68)
        at java.base/java.lang.reflect.Method.invoke(Method.java:583)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1612)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1612)
[ERROR] org.commonmark.internal.LinkReferenceDefinitionParserTest.testRemoveLinesWithoutParagraphLinesPreservesCommittedReferenceSourceSpans -- Time elapsed: 0.004 s <<< FAILURE!
java.lang.AssertionError:

Expecting empty but was: [SourceSpan{line=0, column=0, input=0, length=11}]
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.LinkReferenceDefinitionParserTest.testRemoveLinesWithoutParagraphLinesPreservesCommittedReferenceSourceSpans(LinkReferenceDefinitionParserTest.java:261)
        at java.base/java.lang.reflect.Method.invoke(Method.java:583)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1612)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1612)
[ERROR] org.commonmark.internal.LinkReferenceDefinitionParserTest.testRemoveLinesPreservesCommittedReferenceSourceSpans -- Time elapsed: 0.002 s <<< FAILURE!
org.opentest4j.AssertionFailedError:

Expecting actual:
  [SourceSpan{line=0, column=0, input=0, length=11},
    SourceSpan{line=1, column=0, input=12, length=6}]
to contain exactly (and in same order):
  [SourceSpan{line=1, column=0, input=12, length=6}]
but some elements were not expected:
  [SourceSpan{line=0, column=0, input=0, length=11}]

        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.LinkReferenceDefinitionParserTest.testRemoveLinesPreservesCommittedReferenceSourceSpans(LinkReferenceDefinitionParserTest.java:243)
        at java.base/java.lang.reflect.Method.invoke(Method.java:583)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1612)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1612)

In commonmark@0982a26, `LinkReferenceDefinitionParser` started keeping committed link-reference source spans at the front of `sourceSpans` (`0..referenceSourceSpanCount`) until `finishReference` runs. When a block parser calls `replaceActiveBlockParser` or `replaceParagraphLines` with more lines than the uncommitted paragraph lines (such as `Integer.MAX_VALUE`), `removeLines` strips past `referenceSourceSpanCount` and `finishReference` throws `IndexOutOfBoundsException`.

To avoid this, clamp `removeLines` to the uncommitted trailing paragraph spans and clear that slice directly.

Here are the failures for the new tests before the prod change:

```
[ERROR] org.commonmark.test.BlockParserFactoryTest.replaceActiveBlockParserAfterLinkReferenceDefinitionWithUnclosedTitle -- Time elapsed: 0.002 s <<< ERROR!
java.lang.IndexOutOfBoundsException: toIndex = 1
        at java.base/java.util.AbstractList.subListRangeCheck(AbstractList.java:509)
        at java.base/java.util.ArrayList.subList(ArrayList.java:1199)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.LinkReferenceDefinitionParser.finishReference(LinkReferenceDefinitionParser.java:300)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.LinkReferenceDefinitionParser.getDefinitions(LinkReferenceDefinitionParser.java:110)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.ParagraphParser.getDefinitions(ParagraphParser.java:53)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.DocumentParser.addDefinitionsFrom(DocumentParser.java:595)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.DocumentParser.finalize(DocumentParser.java:590)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.DocumentParser.closeBlockParsers(DocumentParser.java:577)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.DocumentParser.replaceParagraphLines(DocumentParser.java:553)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.DocumentParser.parseLine(DocumentParser.java:297)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.DocumentParser.parse(DocumentParser.java:138)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.parser.Parser.parse(Parser.java:89)
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.test.BlockParserFactoryTest.replaceActiveBlockParserAfterLinkReferenceDefinitionWithUnclosedTitle(BlockParserFactoryTest.java:68)
        at java.base/java.lang.reflect.Method.invoke(Method.java:583)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1612)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1612)
```

```
[ERROR] org.commonmark.internal.LinkReferenceDefinitionParserTest.testRemoveLinesWithoutParagraphLinesPreservesCommittedReferenceSourceSpans -- Time elapsed: 0.004 s <<< FAILURE!
java.lang.AssertionError:

Expecting empty but was: [SourceSpan{line=0, column=0, input=0, length=11}]
        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.LinkReferenceDefinitionParserTest.testRemoveLinesWithoutParagraphLinesPreservesCommittedReferenceSourceSpans(LinkReferenceDefinitionParserTest.java:261)
        at java.base/java.lang.reflect.Method.invoke(Method.java:583)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1612)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1612)
```

```
[ERROR] org.commonmark.internal.LinkReferenceDefinitionParserTest.testRemoveLinesPreservesCommittedReferenceSourceSpans -- Time elapsed: 0.002 s <<< FAILURE!
org.opentest4j.AssertionFailedError:

Expecting actual:
  [SourceSpan{line=0, column=0, input=0, length=11},
    SourceSpan{line=1, column=0, input=12, length=6}]
to contain exactly (and in same order):
  [SourceSpan{line=1, column=0, input=12, length=6}]
but some elements were not expected:
  [SourceSpan{line=0, column=0, input=0, length=11}]

        at org.commonmark@0.33.1-SNAPSHOT/org.commonmark.internal.LinkReferenceDefinitionParserTest.testRemoveLinesPreservesCommittedReferenceSourceSpans(LinkReferenceDefinitionParserTest.java:243)
        at java.base/java.lang.reflect.Method.invoke(Method.java:583)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1612)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1612)
```

This branch has not been deployed

No deployments
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