From: Fletcher T. Penney Date: Sun, 29 Jan 2017 13:33:39 +0000 (-0500) Subject: FIXED: Avoid pathologic slowdown on nested brackets X-Git-Tag: 0.1.2a^2~18 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5b38ea2874af4c16a1a1917b8297e2416cd5b2a9;p=multimarkdown FIXED: Avoid pathologic slowdown on nested brackets --- diff --git a/src/writer.c b/src/writer.c index f5b847b..9b60658 100644 --- a/src/writer.c +++ b/src/writer.c @@ -1160,11 +1160,29 @@ void parse_brackets(const char * source, scratch_pad * scratch, token * bracket, temp_char = text_inside_pair(source, next); if (temp_char[0] == '\0') { - // Empty label, use first bracket + // Empty label, use first bracket (e.g. implicit link `[foo][]`) free(temp_char); temp_char = text_inside_pair(source, bracket); } } else { + // This may be a simplified implicit link, e.g. `[foo]` + + // But not if it's nested brackets, since it would not + // end up being a valid reference + token * walker = bracket->child; + while (walker) { + switch (walker->type) { + case PAIR_BRACKET: + case PAIR_BRACKET_CITATION: + case PAIR_BRACKET_FOOTNOTE: + case PAIR_BRACKET_VARIABLE: + *final_link = NULL; + return; + } + + walker = walker->next; + } + temp_char = text_inside_pair(source, bracket); // Don't skip tokens temp_short = 0; diff --git a/tools/pathological_tests.sh b/tools/pathological_tests.sh index 329f25d..72402c0 100755 --- a/tools/pathological_tests.sh +++ b/tools/pathological_tests.sh @@ -76,17 +76,15 @@ echo "**x *b**c*

" >> path8.html # Nested brackets -# TODO: Performance hit between 1000 and 10,000 iterations -# Checking each level to see if it's a valid link on the stack -# seq -s " " -f "[" $X > path9.text -# echo -n "a" >> path9.text -# seq -s " " -f "]" $X >> path9.text -# -# echo -n "

" > path9.html -# seq -s " " -f "[" $X >> path9.html -# echo -n "a" >> path9.html -# seq -s " " -f "]" $Y >> path9.html -# echo "]

" >> path9.html +seq -s " " -f "[" $X > path9.text +echo -n "a" >> path9.text +seq -s " " -f "]" $X >> path9.text + +echo -n "

" > path9.html +seq -s " " -f "[" $X >> path9.html +echo -n "a" >> path9.html +seq -s " " -f "]" $Y >> path9.html +echo "]

" >> path9.html # Nested block quotes