Skip to content

Commit 07e9b7d

Browse files
authored
🧪 Fix fuzzing test failures (#254)
From https://github.com/google/oss-fuzz/tree/master/projects/markdown-it-py, fixes issues 55363 and 55367
1 parent 34876b1 commit 07e9b7d

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

markdown_it/rules_block/blockquote.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool):
2323
return False
2424

2525
# check the block quote marker
26-
if state.srcCharCode[pos] != 0x3E: # /* > */
26+
try:
27+
if state.srcCharCode[pos] != 0x3E: # /* > */
28+
return False
29+
except IndexError:
2730
return False
2831
pos += 1
2932

markdown_it/rules_block/fence.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ def fence(state: StateBlock, startLine: int, endLine: int, silent: bool):
6666
# test
6767
break
6868

69-
if state.srcCharCode[pos] != marker:
70-
continue
69+
try:
70+
if state.srcCharCode[pos] != marker:
71+
continue
72+
except IndexError:
73+
break
7174

7275
if state.sCount[nextLine] - state.blkIndent >= 4:
7376
# closing fence should be indented less than 4 spaces

markdown_it/rules_block/hr.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ def hr(state: StateBlock, startLine: int, endLine: int, silent: bool):
2020
if state.sCount[startLine] - state.blkIndent >= 4:
2121
return False
2222

23-
marker = state.srcCharCode[pos]
23+
try:
24+
marker = state.srcCharCode[pos]
25+
except IndexError:
26+
return False
2427
pos += 1
2528

2629
# Check hr marker: /* * */ /* - */ /* _ */

markdown_it/rules_block/list.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ def skipBulletListMarker(state: StateBlock, startLine: int):
1313
pos = state.bMarks[startLine] + state.tShift[startLine]
1414
maximum = state.eMarks[startLine]
1515

16-
marker = state.srcCharCode[pos]
16+
try:
17+
marker = state.srcCharCode[pos]
18+
except IndexError:
19+
return -1
1720
pos += 1
1821
# Check bullet /* * */ /* - */ /* + */
1922
if marker != 0x2A and marker != 0x2D and marker != 0x2B:

tests/test_fuzzer.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
These tests are in response to reports from:
3+
https://github.com/google/oss-fuzz/tree/master/projects/markdown-it-py
4+
5+
In the future, perhaps atheris could be directly used here,
6+
but it was not directly apparent how to integrate it into pytest.
7+
"""
8+
import pytest
9+
10+
from markdown_it import MarkdownIt
11+
12+
TESTS = {
13+
55363: ">```\n>",
14+
55367: ">-\n>\n>",
15+
# 55371: "[](so&#4»0;!" TODO this did not fail
16+
# 55401: "?c_" * 100_000 TODO this did not fail
17+
}
18+
19+
20+
@pytest.mark.parametrize("raw_input", TESTS.values(), ids=TESTS.keys())
21+
def test_fuzzing(raw_input):
22+
md = MarkdownIt()
23+
md.parse(raw_input)
24+
print(md.render(raw_input))

0 commit comments

Comments
 (0)