Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ See the [Contributing Guide](contributing.md) for details.

* Ensure nested elements inside inline comments are properly unescaped (#1571).
* Make the docs build successfully with mkdocstrings-python 2.0 (#1575).
* Fix infinite loop when multiple bogus or unclosed HTML comments appear in input (#1578).

## [3.10.0] - 2025-11-03

Expand Down
8 changes: 6 additions & 2 deletions markdown/htmlparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def __init__(self, md: Markdown, *args, **kwargs):
self.lineno_start_cache = [0]

self.override_comment_update = False
self.override_comment_start = 0

# This calls self.reset
super().__init__(*args, **kwargs)
Expand All @@ -124,6 +125,7 @@ def reset(self):
self._cache: list[str] = []
self.cleandoc: list[str] = []
self.lineno_start_cache = [0]
self.override_comment_start = 0

super().reset()

Expand Down Expand Up @@ -276,15 +278,17 @@ def handle_comment(self, data: str):
i = self.line_offset + self.offset + len(data) + 4
if self.rawdata[i:i + 3] != '-->':
self.handle_data('<')
pos = self.line_offset + self.offset
self.override_comment_start = pos - 1 if self.rawdata[pos - 1:pos] == '<' else pos
self.override_comment_update = True
return
self.handle_empty_tag('<!--{}-->'.format(data), is_block=True)

def updatepos(self, i: int, j: int) -> int:
if self.override_comment_update:
self.override_comment_update = False
i = 0
j = 1
i = self.override_comment_start
j = self.override_comment_start + 1
return super().updatepos(i, j)

def handle_decl(self, data: str):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_syntax/blocks/test_html_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1684,3 +1684,17 @@ def test_noname_tag(self):
"""
)
)

def test_multiple_bogus_comments_no_hang(self):
"""Test that multiple bogus comments (</` patterns) don't cause infinite loop."""
self.assertMarkdownRenders(
'`</` and `</`',
'<p><code>&lt;/</code> and <code>&lt;/</code></p>'
)

def test_multiple_unclosed_comments_no_hang(self):
"""Test that multiple unclosed comments don't cause infinite loop."""
self.assertMarkdownRenders(
'<!-- and <!--',
'<p>&lt;!-- and &lt;!--</p>'
)