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 occurs with a long link URL enclosed in Angle Quotes in a reference link definition #648

Open
yehara opened this issue Feb 28, 2025 · 0 comments

Comments

@yehara
Copy link

yehara commented Feb 28, 2025

Describe the bug

In flexmark 0.64.8, a StackOverflowError occurs when a link URL is enclosed in Angle Quotes in a reference link definition like [definition name]: <link URL>, and the link URL is long. The issue is located in the Parser.

A case where this error occurs is when using the "Copy as Markdown" feature for an image from Google Docs, which generates Markdown like the following:

**![][image1]**

[image1]: <data:image/png;base64,....>

To Reproduce

The issue can be reproduced with the following code:

Parser parser = Parser.builder().build();
String url = "https://example.com/" + "A".repeat(5000);
String markdown = "[link]: <%s>".formatted(url);
parser.parse(markdown);

Expected behavior

The parse method should complete successfully.

Resulting Output

java.lang.StackOverflowError
    at com.vladsch.flexmark.util.sequence.SubSequence.charAt(SubSequence.java:115)
    at java.base/java.lang.Character.codePointAt(Character.java:9320)
    at java.base/java.lang.Character.codePointAt(Character.java:9320)
    at java.base/java.util.regex.Pattern$CharProperty.match(Pattern.java:4106)
    at java.base/java.util.regex.Pattern$Branch.match(Pattern.java:4914)
    at java.base/java.util.regex.Pattern$GroupHead.match(Pattern.java:4969)
    at java.base/java.util.regex.Pattern$Loop.match(Pattern.java:5078)
    at java.base/java.util.regex.Pattern$GroupTail.match(Pattern.java:5000)
    at java.base/java.util.regex.Pattern$BranchConn.match(Pattern.java:4878)
    at java.base/java.util.regex.Pattern$CharProperty.match(Pattern.java:4110)
    at java.base/java.util.regex.Pattern$Branch.match(Pattern.java:4914)
    ...

Additional context
The error occurs at the beginning of InlineParserImpl#parseLinkDestination() in the line BasedSequence res = match(myParsing.LINK_DESTINATION_ANGLES);. It appears that the stack overflow is caused by the regular expression matching process.

Possible Solution
Using a possessive quantifier *+ instead of the greedy quantifier * in Parsing.ST_LINK_DESTINATION_ANGLES_SPC and Parsing.ST_LINK_DESTINATION_ANGLES_NO_SPC might avoid the error. However, there is no certainty that this change is correct.

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

1 participant