Skip to content

Commit 7087180

Browse files
committed
Fix StringIndexOutOfBoundsException on line after tab (#52)
This could occur in the following situation (\t is a tab): - foo \tbar # baz The tab on the bar line is a partially consumed tab, so columnIsInTab would be true. On the next line, we wouldn't reset columnIsInTab. By the time that addLine is called, we would try to get a substring starting from "after the tab", resulting in the StringIndexOutOfBoundsException.
1 parent b29c0ec commit 7087180

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

commonmark/src/main/java/org/commonmark/internal/DocumentParser.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ public class DocumentParser implements ParserState {
3333
*/
3434
private int column = 0;
3535

36+
/**
37+
* if the current column is within a tab character (partially consumed tab)
38+
*/
39+
private boolean columnIsInTab;
40+
3641
private int nextNonSpace = 0;
3742
private int nextNonSpaceColumn = 0;
3843
private int indent = 0;
3944
private boolean blank;
40-
private boolean columnIsInTab;
4145

4246
private final List<BlockParserFactory> blockParserFactories;
4347
private final InlineParserImpl inlineParser;
@@ -144,8 +148,7 @@ private void incorporateLine(CharSequence ln) {
144148
line = Parsing.prepareLine(ln);
145149
index = 0;
146150
column = 0;
147-
nextNonSpace = 0;
148-
nextNonSpaceColumn = 0;
151+
columnIsInTab = false;
149152

150153
// For each containing block, try to parse the associated line start.
151154
// Bail out on failure: container will point to the last matching block.
@@ -286,6 +289,8 @@ private void setNewIndex(int newIndex) {
286289
while (index < newIndex && index != line.length()) {
287290
advance();
288291
}
292+
// If we're going to an index as opposed to a column, we're never within a tab
293+
columnIsInTab = false;
289294
}
290295

291296
private void setNewColumn(int newColumn) {

commonmark/src/test/java/org/commonmark/test/SpecialInputTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,11 @@ public void orderedListMarkerOnly() {
7777
assertRendering("2.", "<ol start=\"2\">\n<li></li>\n</ol>\n");
7878
}
7979

80+
@Test
81+
public void columnIsInTabOnPreviousLine() {
82+
assertRendering("- foo\n\n\tbar\n\n# baz\n",
83+
"<ul>\n<li>\n<p>foo</p>\n<p>bar</p>\n</li>\n</ul>\n<h1>baz</h1>\n");
84+
assertRendering("- foo\n\n\tbar\n# baz\n",
85+
"<ul>\n<li>\n<p>foo</p>\n<p>bar</p>\n</li>\n</ul>\n<h1>baz</h1>\n");
86+
}
8087
}

0 commit comments

Comments
 (0)