Skip to content

Commit 71f63e9

Browse files
committed
Fix quadratic complexity deactivating link openers
1 parent d5c27e7 commit 71f63e9

File tree

5 files changed

+8
-6
lines changed

5 files changed

+8
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
104104
- Fixed quadratic complexity parsing unclosed inline links
105105
- Fixed quadratic complexity finding the bottom opener for emphasis and strikethrough delimiters
106106
- Fixed issue where having 500,000+ delimiters could trigger a [known segmentation fault issue in PHP's garbage collection](https://bugs.php.net/bug.php?id=68606)
107+
- Fixed quadratic complexity deactivating link openers
107108

108109
## [2.4.1] - 2023-08-30
109110

src/Delimiter/Bracket.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public function isImage(): bool
6464
return $this->image;
6565
}
6666

67+
/**
68+
* Only valid in the context of non-images (links)
69+
*/
6770
public function isActive(): bool
6871
{
6972
return $this->active;

src/Delimiter/DelimiterStack.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,8 @@ public function removeEarlierMatches(string $character): void
159159
public function deactivateLinkOpeners(): void
160160
{
161161
$opener = $this->brackets;
162-
while ($opener !== null) {
163-
if (! $opener->isImage()) {
164-
$opener->setActive(false);
165-
}
162+
while ($opener !== null && $opener->isActive()) {
163+
$opener->setActive(false);
166164
$opener = $opener->getPrevious();
167165
}
168166
}

src/Extension/CommonMark/Parser/Inline/CloseBracketParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function parse(InlineParserContext $inlineContext): bool
5252
return false;
5353
}
5454

55-
if (! $opener->isActive()) {
55+
if (! $opener->isImage() && ! $opener->isActive()) {
5656
// no matched opener; remove from stack
5757
$inlineContext->getDelimiterStack()->removeBracket();
5858

tests/unit/Delimiter/DelimiterStackTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public function testDeactivateLinkOpeners(): void
324324
$this->assertSame($bracket3, $stack->getLastBracket());
325325

326326
$this->assertFalse($bracket1->isActive());
327-
$this->assertTrue($bracket2->isActive());
327+
$this->assertFalse($bracket2->isActive());
328328
$this->assertFalse($bracket3->isActive());
329329
}
330330
}

0 commit comments

Comments
 (0)