Improve parsing of inline raw HTML. * Isolate some common code for scanning HTML closer into a new function so most HTML scanner functions reuse the same code. * Improve the scanning for the closer so that on failure we remember the range where no closer is present. So any later scanning attempts may fail early. Fixes #73.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2357568..f7b24e2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,13 @@
# MD4C Change Log
+## Next Version (Work in Progress)
+
+Fixes:
+ * [#73](https://github.com/mity/md4c/issues/73):
+ Some raw HTML inputs could lead to quadratic parsing times.
+
+
## Version 0.3.2
Changes:
diff --git a/md4c/md4c.c b/md4c/md4c.c
index e3fecfb..138f06f 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -149,6 +149,12 @@ struct MD_CTX_tag {
int unresolved_link_head;
int unresolved_link_tail;
+ /* For resolving raw HTML. */
+ OFF html_comment_horizon;
+ OFF html_proc_instr_horizon;
+ OFF html_decl_horizon;
+ OFF html_cdata_horizon;
+
/* For block analysis.
* Notes:
* -- It holds MD_BLOCK as well as MD_LINE structures. After each
@@ -166,15 +172,14 @@ struct MD_CTX_tag {
int n_containers;
int alloc_containers;
- int last_line_has_list_loosening_effect;
- int last_list_item_starts_with_two_blank_lines;
-
/* Minimal indentation to call the block "indented code block". */
unsigned code_indent_offset;
/* Contextual info for line analysis. */
SZ code_fence_length; /* For checking closing fence length. */
int html_block_type; /* For checking closing raw HTML condition. */
+ int last_line_has_list_loosening_effect;
+ int last_list_item_starts_with_two_blank_lines;
};
typedef enum MD_LINETYPE_tag MD_LINETYPE;
@@ -1071,108 +1076,93 @@ done:
}
static int
-md_is_html_comment(MD_CTX* ctx, const MD_LINE* lines, int n_lines, OFF beg, OFF max_end, OFF* p_end)
+md_scan_for_html_closer(MD_CTX* ctx, const MD_CHAR* str, MD_SIZE len,
+ const MD_LINE* lines, int n_lines,
+ OFF beg, OFF max_end, OFF* p_end,
+ OFF* p_scan_horizon)
{
OFF off = beg;
int i = 0;
- MD_ASSERT(CH(beg) == _T('<'));
-
- if(off + 4 >= lines[0].end)
- return FALSE;
- if(CH(off+1) != _T('!') || CH(off+2) != _T('-') || CH(off+3) != _T('-'))
- return FALSE;
- off += 4;
-
- /* ">" and "->" must follow the opening. */
- if(off < lines[0].end && CH(off) == _T('>'))
- return FALSE;
- if(off+1 < lines[0].end && CH(off) == _T('-') && CH(off+1) == _T('>'))
+ if(off < *p_scan_horizon && *p_scan_horizon >= max_end - len) {
+ /* We have already scanned the range up to the max_end and we now
+ * there is nothing to see. */
return FALSE;
+ }
- while(1) {
- while(off + 2 < lines[i].end) {
- if(CH(off) == _T('-') && CH(off+1) == _T('-')) {
- if(CH(off+2) == _T('>')) {
- /* Success. */
- off += 2;
- goto done;
- } else {
- /* "--" is prohibited inside the comment. */
- return FALSE;
- }
+ while(TRUE) {
+ while(off + len <= lines[i].end && off + len <= max_end) {
+ if(md_ascii_eq(STR(off), str, len)) {
+ /* Success. */
+ *p_end = off + len;
+ return TRUE;
}
-
off++;
}
i++;
- if(i >= n_lines)
+ if(off >= max_end || i >= n_lines) {
+ /* Failure. */
+ *p_scan_horizon = off;
return FALSE;
+ }
off = lines[i].beg;
-
- if(off >= max_end)
- return FALSE;
}
-
-done:
- if(off >= max_end)
- return FALSE;
-
- *p_end = off+1;
- return TRUE;
}
static int
-md_is_html_processing_instruction(MD_CTX* ctx, const MD_LINE* lines, int n_lines, OFF beg, OFF max_end, OFF* p_end)
+md_is_html_comment(MD_CTX* ctx, const MD_LINE* lines, int n_lines, OFF beg, OFF max_end, OFF* p_end)
{
OFF off = beg;
- int i = 0;
MD_ASSERT(CH(beg) == _T('<'));
- if(off + 2 >= lines[0].end)
+ if(off + 4 >= lines[0].end)
return FALSE;
- if(CH(off+1) != _T('?'))
+ if(CH(off+1) != _T('!') || CH(off+2) != _T('-') || CH(off+3) != _T('-'))
return FALSE;
- off += 2;
+ off += 4;
- while(1) {
- while(off + 1 < lines[i].end) {
- if(CH(off) == _T('?') && CH(off+1) == _T('>')) {
- /* Success. */
- off++;
- goto done;
- }
+ /* ">" and "->" must not follow the opening. */
+ if(off < lines[0].end && CH(off) == _T('>'))
+ return FALSE;
+ if(off+1 < lines[0].end && CH(off) == _T('-') && CH(off+1) == _T('>'))
+ return FALSE;
- off++;
+ /* HTML comment must not contyain "--", so we scan just for "--" instead
+ * of "-->" and verify manually that '>' follows. */
+ if(md_scan_for_html_closer(ctx, _T("--"), 2,
+ lines, n_lines, off, max_end, p_end, &ctx->html_comment_horizon))
+ {
+ if(*p_end < max_end && CH(*p_end) == _T('>')) {
+ *p_end = *p_end + 1;
+ return TRUE;
}
+ }
- i++;
- if(i >= n_lines)
- return FALSE;
+ return FALSE;
+}
- off = lines[i].beg;
- if(off >= max_end)
- return FALSE;
- }
+static int
+md_is_html_processing_instruction(MD_CTX* ctx, const MD_LINE* lines, int n_lines, OFF beg, OFF max_end, OFF* p_end)
+{
+ OFF off = beg;
-done:
- if(off >= max_end)
+ if(off + 2 >= lines[0].end)
+ return FALSE;
+ if(CH(off+1) != _T('?'))
return FALSE;
+ off += 2;
- *p_end = off+1;
- return TRUE;
+ return md_scan_for_html_closer(ctx, _T("?>"), 2,
+ lines, n_lines, off, max_end, p_end, &ctx->html_proc_instr_horizon);
}
static int
md_is_html_declaration(MD_CTX* ctx, const MD_LINE* lines, int n_lines, OFF beg, OFF max_end, OFF* p_end)
{
OFF off = beg;
- int i = 0;
-
- MD_ASSERT(CH(beg) == _T('<'));
if(off + 2 >= lines[0].end)
return FALSE;
@@ -1189,31 +1179,8 @@ md_is_html_declaration(MD_CTX* ctx, const MD_LINE* lines, int n_lines, OFF beg,
if(off < lines[0].end && !ISWHITESPACE(off))
return FALSE;
- while(1) {
- while(off < lines[i].end) {
- if(CH(off) == _T('>')) {
- /* Success. */
- goto done;
- }
-
- off++;
- }
-
- i++;
- if(i >= n_lines)
- return FALSE;
-
- off = lines[i].beg;
- if(off >= max_end)
- return FALSE;
- }
-
-done:
- if(off >= max_end)
- return FALSE;
-
- *p_end = off+1;
- return TRUE;
+ return md_scan_for_html_closer(ctx, _T(">"), 1,
+ lines, n_lines, off, max_end, p_end, &ctx->html_decl_horizon);
}
static int
@@ -1223,7 +1190,6 @@ md_is_html_cdata(MD_CTX* ctx, const MD_LINE* lines, int n_lines, OFF beg, OFF ma
static const SZ open_size = SIZEOF_ARRAY(open_str) - 1;
OFF off = beg;
- int i = 0;
if(off + open_size >= lines[0].end)
return FALSE;
@@ -1231,49 +1197,22 @@ md_is_html_cdata(MD_CTX* ctx, const MD_LINE* lines, int n_lines, OFF beg, OFF ma
return FALSE;
off += open_size;
- while(1) {
- while(off + 2 < lines[i].end) {
- if(CH(off) == _T(']') && CH(off+1) == _T(']') && CH(off+2) == _T('>')) {
- /* Success. */
- off += 2;
- goto done;
- }
-
- off++;
- }
+ if(lines[n_lines-1].end < max_end)
+ max_end = lines[n_lines-1].end - 2;
- i++;
- if(i >= n_lines)
- return FALSE;
-
- off = lines[i].beg;
- if(off >= max_end)
- return FALSE;
- }
-
-done:
- if(off >= max_end)
- return FALSE;
-
- *p_end = off+1;
- return TRUE;
+ return md_scan_for_html_closer(ctx, _T("]]>"), 3,
+ lines, n_lines, off, max_end, p_end, &ctx->html_cdata_horizon);
}
static int
md_is_html_any(MD_CTX* ctx, const MD_LINE* lines, int n_lines, OFF beg, OFF max_end, OFF* p_end)
{
- if(md_is_html_tag(ctx, lines, n_lines, beg, max_end, p_end) == TRUE)
- return TRUE;
- if(md_is_html_comment(ctx, lines, n_lines, beg, max_end, p_end) == TRUE)
- return TRUE;
- if(md_is_html_processing_instruction(ctx, lines, n_lines, beg, max_end, p_end) == TRUE)
- return TRUE;
- if(md_is_html_declaration(ctx, lines, n_lines, beg, max_end, p_end) == TRUE)
- return TRUE;
- if(md_is_html_cdata(ctx, lines, n_lines, beg, max_end, p_end) == TRUE)
- return TRUE;
-
- return FALSE;
+ MD_ASSERT(CH(beg) == _T('<'));
+ return (md_is_html_tag(ctx, lines, n_lines, beg, max_end, p_end) ||
+ md_is_html_comment(ctx, lines, n_lines, beg, max_end, p_end) ||
+ md_is_html_processing_instruction(ctx, lines, n_lines, beg, max_end, p_end) ||
+ md_is_html_declaration(ctx, lines, n_lines, beg, max_end, p_end) ||
+ md_is_html_cdata(ctx, lines, n_lines, beg, max_end, p_end));
}
diff --git a/test/pathological_tests.py b/test/pathological_tests.py
index 8518032..6dd2b40 100755
--- a/test/pathological_tests.py
+++ b/test/pathological_tests.py
@@ -75,6 +75,12 @@ pathological = {
"many html openers and closers":
(("<>" * 50000),
re.compile("(<>){50000}")),
+ "many html proc. inst. openers":
+ (("x" + "<?" * 50000),
+ re.compile("x(<\\?){50000}")),
+ "many html CDATA openers":
+ (("x" + "<![CDATA[" * 50000),
+ re.compile("x(<!\\[CDATA\\[){50000}")),
"many backticks and escapes":
(("\\``" * 50000),
re.compile("(``){50000}")),