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 with long base64 image and LINKS_ALLOW_MATCHED_PARENTHESES disabled #381

Closed
mrombout opened this issue Dec 19, 2019 · 3 comments

Comments

@mrombout
Copy link

Describe the bug

Most likely the same, or similar issue as #364.

Using a Parser with LINKS_ALLOW_MATCHED_PARENTHESES = false.

When parsing a reasonably sized base64 image in markdown the parser has trouble matching and ends up with a StackOverflowError. Decreasing the size of the base64 image will eventually allow the regex to cope.

I found this because for the time being, I am using PegdownOptionsAdapter, which has that option enabled by default.

To Reproduce

Running:

openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment (build 11.0.5+10-post-Ubuntu-0ubuntu1.118.04)
OpenJDK 64-Bit Server VM (build 11.0.5+10-post-Ubuntu-0ubuntu1.118.04, mixed mode, sharing)

See https://github.com/mrombout/flexmark-so-sscce, or below.

MutableDataSet options = new MutableDataSet().set(Parser.LINKS_ALLOW_MATCHED_PARENTHESES, false);

Parser parser = Parser.builder(options).build();
HtmlRenderer renderer = HtmlRenderer.builder(options).build();

Document parse = parser.parse("![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAHsklEQVR42u1bC1BUVRi+ZGKapfaUpgzHxrGcAlr23gVB0MiATMkHmqmgxpIoyt27PESRVTFJhUVFXkoQhpPmA/M9ZjKVmpqmNtNjcprGnB7WFDNWM5VxOv/lHl1pd++5955dhPHOfMMwe+855/vP/zrn/Ifjbj23nk7zmM3mUIvFEuuKLk04NDS0LyadyvN8MwbygrOCIGTB+12GPCblwGhRId4e8H7Szccm2zSIE83JnI13YLzJicI+/LcZ4yQnCYfw3x349zLOZp4dlGxOw7N+XiPxG9DxpjFxYjdO4p/jbEINJ/LfYYKIBr3SeWSKdD+ziv0Hg7pTCKGpY4hnYRuU+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ee3gcvwcTasKzeU7ZbQKCLRg/YlzARI/IR1gw05Jg6lDS7QRAc3OrCW5rebrPA1rBddaH5nqKN+3oEpcbMZF6Hff3HF3tImOSouqeSIPtN4G6dyni3jJCBcHcrafzPv8B13NkQkVaYmEAAAAASUVORK5CYII=)\n");
String render = renderer.render(parse);

System.out.println(render);

Expected behavior

Output an image tag with the base64 data.

<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAHsklEQVR42u1bC1BUVRi+ZGKapfaUpgzHxrGcAlr23gVB0MiATMkHmqmgxpIoyt27PESRVTFJhUVFXkoQhpPmA/M9ZjKVmpqmNtNjcprGnB7WFDNWM5VxOv/lHl1pd++5955dhPHOfMMwe+855/vP/zrn/Ifjbj23nk7zmM3mUIvFEuuKLk04NDS0LyadyvN8MwbygrOCIGTB+12GPCblwGhRId4e8H7Szccm2zSIE83JnI13YLzJicI+/LcZ4yQnCYfw3x349zLOZp4dlGxOw7N+XiPxG9DxpjFxYjdO4p/jbEINJ/LfYYKIBr3SeWSKdD+ziv0Hg7pTCKGpY4hnYRuU+HxM5qIrsTvzolGYcwJ6vi4NzdpuR9K+xSj/0FLkeP81lHdwCZq3eyGaUGVFQpTFLaFHx/IXZIHiRxGCqmn4l7hjaCAeYA4m+xshfW/BSJTU8Cpa2lyMGs5vQJs+2+gVieMTPJLpkaEIU+KPcXaLoPiHm0QAIh+NB/c5IT64eAyy7y9EDSqEXSGVzvdIZGT8CDR+UwbqvWA40ah/A+cIdfi3ix1rAmDnNr4Q4yoMrL/jWZRzwEFNmmDjiQoUFRPlUQCT05Pl9zacqUSj66zodrulzazS+Eu8wF9x9w34C9+Sz4jtjQdxAAYSIAloTH06qjtXo5k8YG7RbK+qDL+7vl/84So0cHlim9O08n+FRAlfuOYDviefGXa/HL7wAO7KH65r1mlnH1C8ben/vqs7W43ialKJSbTiECr5x95zTX1wrD4PHT9QGIdWHy3VTR6QX5WjGtNBSJ6+n/L2PBRAo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HSOsJkCeUfVxGzgTA+49iIwA7P1A+ddWxQ0yTALHK/yFcP1kyjnj+LYyPxgU7NAyFSiBllgkQq/B3TfUl/hcuZ9hDrA/IA4hDDF6eiKpPVzBLgFioP9QdKoeieBUrJPqmQEIy3Yc7+BqE8MSqJLlOx+gOEAvvv/jwchRI7B5yF9/WBMop8g9ECDVnKgwlQEZ3f6Aescd18m9p3//XJQTzUNzZ99DpgKIE+QBCTwJEU/+jttvTTakZ5ES+Qa5b9F9NsCUYd/wVcYxQ66vV/vXGftC66Mqp14usJWG1f2ZeucAApejyxYS20pmdZCBhzonyqSyN/eud/YxdeajvolhC/gomP81vV1Xc3NpoDo8ID8NhJ1MeDB5U9+wIuUJj8bZCr7MP5qElvot7C9CgFaNdS+tPaT/uMkbeU3l6i6wN800D8KA2QyEzDDDoJcEjeUiNaYhDUQYUSUMNsgvxy9gHZfjV3tXu68BdnXYOsjE0mm91925EdCTK21Egk6s6vV4GrDFWHS1BuQcdaPpWEcVUTpOrUG+4TCFfusD9WE29OuLujlo4a26nMame3u2XQndzRMGvcmgTzfH+9fDaBdBCbmvBX/z/t+7ee3gcvwcTasKzeU7ZbQKCLRg/YlzARI/IR1gw05Jg6lDS7QRAc3OrCW5rebrPA1rBddaH5nqKN+3oEpcbMZF6Hff3HF3tImOSouqeSIPtN4G6dyni3jJCBcHcrafzPv8B13NkQkVaYmEAAAAASUVORK5CYII=" alt="" /></p>

Additional context

Enabling Parser.LINKS_ALLOW_MATCHED_PARENTHESES avoid the regular expressions which I think is the culprit in InlineParserImpl#parseLinkDestination:1168:

if (options.linksAllowMatchedParentheses) {
    // ... 
} else {
    // spec 0.27 compatibility
    BasedSequence matched = match(myParsing.LINK_DESTINATION);
    return matched != null && spaceInUrls ? matched.trimEnd(BasedSequence.SPACE) : matched;
}
@mrombout mrombout changed the title Stack StackOverflowError with long base64 image and LINKS_ALLOW_MATCHED_PARENTHESES disabled Dec 19, 2019
@vsch
Copy link
Owner

vsch commented Dec 19, 2019

@mrombout, I think you are right.

I have been trying to figure out which option causes this and even have a hand-rolled parser to replace the regex one, which does not have the issue. However, in some cases the stack overflow is still caused and I have not been able to isolate exactly what causes this.

I will take another look through the code to see what other parts of code use this regex but in the mean time I will disable this feature by default.

@vsch vsch added the 🪲 bug label Dec 19, 2019
@vsch
Copy link
Owner

vsch commented Dec 20, 2019

@mrombout, my bad. I completely forgot about the matched parens option and my hand-rolled parser, which does not suffer from stack overflow, is bypassed if this option is not selected. Thus the continued stack overflow occurrences.

I am adding a flag to the state machine parser for no parens and will have it parsing in both cases without regex.

Thank you for bringing this up. It was on my to do list and nagging me to figure it out. Your finding pointed me in the right direction.

@vsch
Copy link
Owner

vsch commented Dec 20, 2019

Fix for this is available. Repo updated, maven updated but may take a while to show up in maven central.

@vsch vsch closed this as completed Dec 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants