Skip to content

Commit

Permalink
Fix StringIndexOutOfBoundsException on line after tab (#52)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
robinst committed May 25, 2016
1 parent b29c0ec commit 7087180
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ public class DocumentParser implements ParserState {
*/
private int column = 0;

/**
* if the current column is within a tab character (partially consumed tab)
*/
private boolean columnIsInTab;

private int nextNonSpace = 0;
private int nextNonSpaceColumn = 0;
private int indent = 0;
private boolean blank;
private boolean columnIsInTab;

private final List<BlockParserFactory> blockParserFactories;
private final InlineParserImpl inlineParser;
Expand Down Expand Up @@ -144,8 +148,7 @@ private void incorporateLine(CharSequence ln) {
line = Parsing.prepareLine(ln);
index = 0;
column = 0;
nextNonSpace = 0;
nextNonSpaceColumn = 0;
columnIsInTab = false;

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

private void setNewColumn(int newColumn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ public void orderedListMarkerOnly() {
assertRendering("2.", "<ol start=\"2\">\n<li></li>\n</ol>\n");
}

@Test
public void columnIsInTabOnPreviousLine() {
assertRendering("- foo\n\n\tbar\n\n# baz\n",
"<ul>\n<li>\n<p>foo</p>\n<p>bar</p>\n</li>\n</ul>\n<h1>baz</h1>\n");
assertRendering("- foo\n\n\tbar\n# baz\n",
"<ul>\n<li>\n<p>foo</p>\n<p>bar</p>\n</li>\n</ul>\n<h1>baz</h1>\n");
}
}

0 comments on commit 7087180

Please sign in to comment.