Skip to content

Commit 9144d3a

Browse files
jgmkevinbackhouse
authored andcommitted
Fix quadratic parsing issue with repeated <!--.
We handle this the same way we handle the parallel issue with declarations: if we reach the end of input without seeing `-->`, we set a flag telling us not to try again. Resolves GHSL-2022-098
1 parent 39ca0d9 commit 9144d3a

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/inlines.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ typedef struct bracket {
4848
#define FLAG_SKIP_HTML_CDATA (1u << 0)
4949
#define FLAG_SKIP_HTML_DECLARATION (1u << 1)
5050
#define FLAG_SKIP_HTML_PI (1u << 2)
51+
#define FLAG_SKIP_HTML_COMMENT (1u << 3)
5152

5253
typedef struct subject{
5354
cmark_mem *mem;
@@ -907,7 +908,7 @@ static cmark_node *handle_pointy_brace(subject *subj, int options) {
907908
// finally, try to match an html tag
908909
if (subj->pos + 2 <= subj->input.len) {
909910
int c = subj->input.data[subj->pos];
910-
if (c == '!') {
911+
if (c == '!' && (subj->flags & FLAG_SKIP_HTML_COMMENT) == 0) {
911912
c = subj->input.data[subj->pos+1];
912913
if (c == '-' && subj->input.data[subj->pos+2] == '-') {
913914
if (subj->input.data[subj->pos+3] == '>') {
@@ -917,8 +918,12 @@ static cmark_node *handle_pointy_brace(subject *subj, int options) {
917918
matchlen = 5;
918919
} else {
919920
matchlen = scan_html_comment(&subj->input, subj->pos + 1);
920-
if (matchlen > 0)
921+
if (matchlen > 0) {
921922
matchlen += 1; // prefix "<"
923+
} else { // no match through end of input: set a flag so
924+
// we don't reparse looking for -->:
925+
subj->flags |= FLAG_SKIP_HTML_COMMENT;
926+
}
922927
}
923928
} else if (c == '[') {
924929
if ((subj->flags & FLAG_SKIP_HTML_CDATA) == 0) {

0 commit comments

Comments
 (0)