Skip to content

Commit

Permalink
Fix index out of bounds error in code span parsing
Browse files Browse the repository at this point in the history
We were labouring under false assumption that tree siblings have
consecutive tree indices. This is only _almost_ always true ;-)
  • Loading branch information
marcusklaas committed Jan 10, 2022
1 parent fba26ff commit 60d3656
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,8 +743,7 @@ impl<'input, 'callback> Parser<'input, 'callback> {
///
/// Both `open` and `close` are matching MaybeCode items.
fn make_code_span(&mut self, open: TreeIndex, close: TreeIndex, preceding_backslash: bool) {
let first_ix = open + 1;
let last_ix = close - 1;
let first_ix = self.tree[open].next.unwrap();
let bytes = self.text.as_bytes();
let mut span_start = self.tree[open].item.end;
let mut span_end = self.tree[close].item.start;
Expand All @@ -765,17 +764,17 @@ impl<'input, 'callback> Parser<'input, 'callback> {

let mut ix = first_ix;

while ix < close {
while ix != close {
let next_ix = self.tree[ix].next.unwrap();
if let ItemBody::HardBreak | ItemBody::SoftBreak = self.tree[ix].item.body {
if drop_enclosing_whitespace {
// check whether break should be ignored
if ix == first_ix {
ix = ix + 1;
ix = next_ix;
span_start = min(span_end, self.tree[ix].item.start);
continue;
} else if ix == last_ix && last_ix > first_ix {
ix = ix + 1;
continue;
} else if next_ix == close && ix > first_ix {
break;
}
}

Expand All @@ -794,14 +793,14 @@ impl<'input, 'callback> Parser<'input, 'callback> {
buf = Some(new_buf);
}
} else if let Some(ref mut buf) = buf {
let end = if ix == last_ix {
let end = if next_ix == close {
span_end
} else {
self.tree[ix].item.end
};
buf.push_str(&self.text[self.tree[ix].item.start..end]);
}
ix = ix + 1;
ix = next_ix;
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ fn parse(md: &str) {
for _ in parser {}
}

#[test]
fn test_lists_inside_code_spans() {
parse(
r"- `
x
**
*
`",
);
}

#[test]
fn test_wrong_code_block() {
parse(
Expand Down

0 comments on commit 60d3656

Please sign in to comment.