diff --git a/src/wp-includes/html-api/class-wp-html-tag-processor.php b/src/wp-includes/html-api/class-wp-html-tag-processor.php
index 7a53fbea1e273..fcf418e856d5e 100644
--- a/src/wp-includes/html-api/class-wp-html-tag-processor.php
+++ b/src/wp-includes/html-api/class-wp-html-tag-processor.php
@@ -1909,6 +1909,8 @@ private function parse_next_tag(): bool {
if ( $this->is_closing_tag ) {
// No chance of finding a closer.
if ( $at + 3 > $doc_length ) {
+ $this->parser_state = self::STATE_INCOMPLETE_INPUT;
+
return false;
}
diff --git a/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php b/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php
index 637aa38751688..b9c6817988032 100644
--- a/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php
+++ b/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php
@@ -2903,4 +2903,15 @@ public function test_script_tag_processing_no_infinite_loop_final_left_angle_bra
$this->assertFalse( $processor->next_tag() );
$this->assertTrue( $processor->paused_at_incomplete_token() );
}
+
+ /**
+ * Test a bugfix where the input ends abruptly with a funky comment started.
+ *
+ * @ticket 61831
+ */
+ public function test_unclosed_funky_comment_input_too_short() {
+ $processor = new WP_HTML_Tag_Processor( '#' );
+ $this->assertFalse( $processor->next_tag() );
+ $this->assertTrue( $processor->paused_at_incomplete_token() );
+ }
}