commonmarker 0.23.7.pre1 → 0.23.8
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of commonmarker might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/ext/commonmarker/arena.c +9 -8
- data/ext/commonmarker/autolink.c +207 -155
- data/ext/commonmarker/blocks.c +14 -0
- data/ext/commonmarker/cmark-gfm-core-extensions.h +11 -11
- data/ext/commonmarker/cmark-gfm-extension_api.h +1 -0
- data/ext/commonmarker/cmark-gfm.h +7 -2
- data/ext/commonmarker/cmark.c +3 -3
- data/ext/commonmarker/commonmarker.c +1 -0
- data/ext/commonmarker/html.c +16 -2
- data/ext/commonmarker/inlines.c +130 -58
- data/ext/commonmarker/map.c +11 -4
- data/ext/commonmarker/map.h +5 -2
- data/ext/commonmarker/node.c +30 -0
- data/ext/commonmarker/node.h +27 -1
- data/ext/commonmarker/parser.h +1 -0
- data/ext/commonmarker/references.c +1 -0
- data/ext/commonmarker/scanners.c +13916 -10368
- data/ext/commonmarker/scanners.h +8 -0
- data/ext/commonmarker/strikethrough.c +1 -1
- data/ext/commonmarker/table.c +59 -35
- data/lib/commonmarker/version.rb +1 -1
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5720fe7353e4f737164cc06dd97f54d8e203342bcaf2267b77e26231122ef65
|
4
|
+
data.tar.gz: 1b929f138c087ce5da68ad0cdf61701ef0521db9e5d13c973b0ae81ea3c25bb1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2867a5effac7f5a52f1f850e8c9f9f4257105e14fafd34ea5bb688bf98357aa15aece3e55cc8df27df219624493d41e34a715dba4f631b2618e294dee30e61a7
|
7
|
+
data.tar.gz: 938dfa5d2335d7ec2f9ee330b2d1c502c6c95809f8835ee1b6e1ba9c9bce13e5e73315f7add0feab39e229085266e416c8865f77b66731f915aeee78e87eb5ac
|
data/ext/commonmarker/arena.c
CHANGED
@@ -68,15 +68,16 @@ static void *arena_calloc(size_t nmem, size_t size) {
|
|
68
68
|
const size_t align = sizeof(size_t) - 1;
|
69
69
|
sz = (sz + align) & ~align;
|
70
70
|
|
71
|
+
struct arena_chunk *chunk;
|
71
72
|
if (sz > A->sz) {
|
72
|
-
A->prev = alloc_arena_chunk(sz, A->prev);
|
73
|
-
|
73
|
+
A->prev = chunk = alloc_arena_chunk(sz, A->prev);
|
74
|
+
} else if (sz > A->sz - A->used) {
|
75
|
+
A = chunk = alloc_arena_chunk(A->sz + A->sz / 2, A);
|
76
|
+
} else {
|
77
|
+
chunk = A;
|
74
78
|
}
|
75
|
-
|
76
|
-
|
77
|
-
}
|
78
|
-
void *ptr = (uint8_t *) A->ptr + A->used;
|
79
|
-
A->used += sz;
|
79
|
+
void *ptr = (uint8_t *) chunk->ptr + chunk->used;
|
80
|
+
chunk->used += sz;
|
80
81
|
*((size_t *) ptr) = sz - sizeof(size_t);
|
81
82
|
return (uint8_t *) ptr + sizeof(size_t);
|
82
83
|
}
|
@@ -98,6 +99,6 @@ static void arena_free(void *ptr) {
|
|
98
99
|
|
99
100
|
cmark_mem CMARK_ARENA_MEM_ALLOCATOR = {arena_calloc, arena_realloc, arena_free};
|
100
101
|
|
101
|
-
cmark_mem *cmark_get_arena_mem_allocator() {
|
102
|
+
cmark_mem *cmark_get_arena_mem_allocator(void) {
|
102
103
|
return &CMARK_ARENA_MEM_ALLOCATOR;
|
103
104
|
}
|
data/ext/commonmarker/autolink.c
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
#include <parser.h>
|
3
3
|
#include <string.h>
|
4
4
|
#include <utf8.h>
|
5
|
+
#include <stddef.h>
|
5
6
|
|
6
7
|
#if defined(_WIN32)
|
7
8
|
#define strncasecmp _strnicmp
|
@@ -35,44 +36,25 @@ static int sd_autolink_issafe(const uint8_t *link, size_t link_len) {
|
|
35
36
|
}
|
36
37
|
|
37
38
|
static size_t autolink_delim(uint8_t *data, size_t link_end) {
|
38
|
-
uint8_t cclose, copen;
|
39
39
|
size_t i;
|
40
|
+
size_t closing = 0;
|
41
|
+
size_t opening = 0;
|
40
42
|
|
41
|
-
for (i = 0; i < link_end; ++i)
|
42
|
-
|
43
|
+
for (i = 0; i < link_end; ++i) {
|
44
|
+
const uint8_t c = data[i];
|
45
|
+
if (c == '<') {
|
43
46
|
link_end = i;
|
44
47
|
break;
|
48
|
+
} else if (c == '(') {
|
49
|
+
opening++;
|
50
|
+
} else if (c == ')') {
|
51
|
+
closing++;
|
45
52
|
}
|
53
|
+
}
|
46
54
|
|
47
55
|
while (link_end > 0) {
|
48
|
-
|
49
|
-
|
50
|
-
switch (cclose) {
|
56
|
+
switch (data[link_end - 1]) {
|
51
57
|
case ')':
|
52
|
-
copen = '(';
|
53
|
-
break;
|
54
|
-
default:
|
55
|
-
copen = 0;
|
56
|
-
}
|
57
|
-
|
58
|
-
if (strchr("?!.,:*_~'\"", data[link_end - 1]) != NULL)
|
59
|
-
link_end--;
|
60
|
-
|
61
|
-
else if (data[link_end - 1] == ';') {
|
62
|
-
size_t new_end = link_end - 2;
|
63
|
-
|
64
|
-
while (new_end > 0 && cmark_isalpha(data[new_end]))
|
65
|
-
new_end--;
|
66
|
-
|
67
|
-
if (new_end < link_end - 2 && data[new_end] == '&')
|
68
|
-
link_end = new_end;
|
69
|
-
else
|
70
|
-
link_end--;
|
71
|
-
} else if (copen != 0) {
|
72
|
-
size_t closing = 0;
|
73
|
-
size_t opening = 0;
|
74
|
-
i = 0;
|
75
|
-
|
76
58
|
/* Allow any number of matching brackets (as recognised in copen/cclose)
|
77
59
|
* at the end of the URL. If there is a greater number of closing
|
78
60
|
* brackets than opening ones, we remove one character from the end of
|
@@ -80,34 +62,52 @@ static size_t autolink_delim(uint8_t *data, size_t link_end) {
|
|
80
62
|
*
|
81
63
|
* Examples (input text => output linked portion):
|
82
64
|
*
|
83
|
-
*
|
84
|
-
*
|
65
|
+
* http://www.pokemon.com/Pikachu_(Electric)
|
66
|
+
* => http://www.pokemon.com/Pikachu_(Electric)
|
85
67
|
*
|
86
|
-
*
|
87
|
-
*
|
68
|
+
* http://www.pokemon.com/Pikachu_((Electric)
|
69
|
+
* => http://www.pokemon.com/Pikachu_((Electric)
|
88
70
|
*
|
89
|
-
*
|
90
|
-
*
|
71
|
+
* http://www.pokemon.com/Pikachu_(Electric))
|
72
|
+
* => http://www.pokemon.com/Pikachu_(Electric)
|
91
73
|
*
|
92
|
-
*
|
93
|
-
*
|
74
|
+
* http://www.pokemon.com/Pikachu_((Electric))
|
75
|
+
* => http://www.pokemon.com/Pikachu_((Electric))
|
94
76
|
*/
|
95
|
-
|
96
|
-
|
97
|
-
if (data[i] == copen)
|
98
|
-
opening++;
|
99
|
-
else if (data[i] == cclose)
|
100
|
-
closing++;
|
101
|
-
|
102
|
-
i++;
|
77
|
+
if (closing <= opening) {
|
78
|
+
return link_end;
|
103
79
|
}
|
80
|
+
closing--;
|
81
|
+
link_end--;
|
82
|
+
break;
|
83
|
+
case '?':
|
84
|
+
case '!':
|
85
|
+
case '.':
|
86
|
+
case ',':
|
87
|
+
case ':':
|
88
|
+
case '*':
|
89
|
+
case '_':
|
90
|
+
case '~':
|
91
|
+
case '\'':
|
92
|
+
case '"':
|
93
|
+
link_end--;
|
94
|
+
break;
|
95
|
+
case ';': {
|
96
|
+
size_t new_end = link_end - 2;
|
104
97
|
|
105
|
-
|
106
|
-
|
98
|
+
while (new_end > 0 && cmark_isalpha(data[new_end]))
|
99
|
+
new_end--;
|
107
100
|
|
108
|
-
link_end
|
109
|
-
|
101
|
+
if (new_end < link_end - 2 && data[new_end] == '&')
|
102
|
+
link_end = new_end;
|
103
|
+
else
|
104
|
+
link_end--;
|
110
105
|
break;
|
106
|
+
}
|
107
|
+
|
108
|
+
default:
|
109
|
+
return link_end;
|
110
|
+
}
|
111
111
|
}
|
112
112
|
|
113
113
|
return link_end;
|
@@ -116,7 +116,20 @@ static size_t autolink_delim(uint8_t *data, size_t link_end) {
|
|
116
116
|
static size_t check_domain(uint8_t *data, size_t size, int allow_short) {
|
117
117
|
size_t i, np = 0, uscore1 = 0, uscore2 = 0;
|
118
118
|
|
119
|
+
/* The purpose of this code is to reject urls that contain an underscore
|
120
|
+
* in one of the last two segments. Examples:
|
121
|
+
*
|
122
|
+
* www.xxx.yyy.zzz autolinked
|
123
|
+
* www.xxx.yyy._zzz not autolinked
|
124
|
+
* www.xxx._yyy.zzz not autolinked
|
125
|
+
* www._xxx.yyy.zzz autolinked
|
126
|
+
*
|
127
|
+
* The reason is that domain names are allowed to include underscores,
|
128
|
+
* but host names are not. See: https://stackoverflow.com/a/2183140
|
129
|
+
*/
|
119
130
|
for (i = 1; i < size - 1; i++) {
|
131
|
+
if (data[i] == '\\' && i < size - 2)
|
132
|
+
i++;
|
120
133
|
if (data[i] == '_')
|
121
134
|
uscore2++;
|
122
135
|
else if (data[i] == '.') {
|
@@ -127,8 +140,17 @@ static size_t check_domain(uint8_t *data, size_t size, int allow_short) {
|
|
127
140
|
break;
|
128
141
|
}
|
129
142
|
|
130
|
-
if (uscore1 > 0 || uscore2 > 0)
|
131
|
-
|
143
|
+
if (uscore1 > 0 || uscore2 > 0) {
|
144
|
+
/* If the url is very long then accept it despite the underscores,
|
145
|
+
* to avoid quadratic behavior causing a denial of service. See:
|
146
|
+
* https://github.com/github/cmark-gfm/security/advisories/GHSA-29g3-96g3-jg6c
|
147
|
+
* Reasonable urls are unlikely to have more than 10 segments, so
|
148
|
+
* this extra condition shouldn't have any impact on normal usage.
|
149
|
+
*/
|
150
|
+
if (np <= 10) {
|
151
|
+
return 0;
|
152
|
+
}
|
153
|
+
}
|
132
154
|
|
133
155
|
if (allow_short) {
|
134
156
|
/* We don't need a valid domain in the strict sense (with
|
@@ -165,7 +187,7 @@ static cmark_node *www_match(cmark_parser *parser, cmark_node *parent,
|
|
165
187
|
if (link_end == 0)
|
166
188
|
return NULL;
|
167
189
|
|
168
|
-
while (link_end < size && !cmark_isspace(data[link_end]))
|
190
|
+
while (link_end < size && !cmark_isspace(data[link_end]) && data[link_end] != '<')
|
169
191
|
link_end++;
|
170
192
|
|
171
193
|
link_end = autolink_delim(data, link_end);
|
@@ -225,7 +247,7 @@ static cmark_node *url_match(cmark_parser *parser, cmark_node *parent,
|
|
225
247
|
return 0;
|
226
248
|
|
227
249
|
link_end += domain_len;
|
228
|
-
while (link_end < size && !cmark_isspace(data[link_end]))
|
250
|
+
while (link_end < size && !cmark_isspace(data[link_end]) && data[link_end] != '<')
|
229
251
|
link_end++;
|
230
252
|
|
231
253
|
link_end = autolink_delim(data, link_end);
|
@@ -245,6 +267,11 @@ static cmark_node *url_match(cmark_parser *parser, cmark_node *parent,
|
|
245
267
|
cmark_node *text = cmark_node_new_with_mem(CMARK_NODE_TEXT, parser->mem);
|
246
268
|
text->as.literal = url;
|
247
269
|
cmark_node_append_child(node, text);
|
270
|
+
|
271
|
+
node->start_line = text->start_line = node->end_line = text->end_line = cmark_inline_parser_get_line(inline_parser);
|
272
|
+
|
273
|
+
node->start_column = text->start_column = max_rewind - rewind;
|
274
|
+
node->end_column = text->end_column = cmark_inline_parser_get_column(inline_parser) - 1;
|
248
275
|
|
249
276
|
return node;
|
250
277
|
}
|
@@ -269,142 +296,167 @@ static cmark_node *match(cmark_syntax_extension *ext, cmark_parser *parser,
|
|
269
296
|
// inline was finished in inlines.c.
|
270
297
|
}
|
271
298
|
|
272
|
-
static bool validate_protocol(char protocol[], uint8_t *data,
|
299
|
+
static bool validate_protocol(char protocol[], uint8_t *data, size_t rewind, size_t max_rewind) {
|
273
300
|
size_t len = strlen(protocol);
|
274
301
|
|
302
|
+
if (len > (max_rewind - rewind)) {
|
303
|
+
return false;
|
304
|
+
}
|
305
|
+
|
275
306
|
// Check that the protocol matches
|
276
|
-
|
277
|
-
|
278
|
-
return false;
|
279
|
-
}
|
307
|
+
if (memcmp(data - rewind - len, protocol, len) != 0) {
|
308
|
+
return false;
|
280
309
|
}
|
281
310
|
|
282
|
-
|
311
|
+
if (len == (max_rewind - rewind)) {
|
312
|
+
return true;
|
313
|
+
}
|
314
|
+
|
315
|
+
char prev_char = data[-((ptrdiff_t)rewind) - len - 1];
|
283
316
|
|
284
317
|
// Make sure the character before the protocol is non-alphanumeric
|
285
318
|
return !cmark_isalnum(prev_char);
|
286
319
|
}
|
287
320
|
|
288
|
-
static void postprocess_text(cmark_parser *parser, cmark_node *text
|
289
|
-
|
290
|
-
|
291
|
-
|
321
|
+
static void postprocess_text(cmark_parser *parser, cmark_node *text) {
|
322
|
+
size_t start = 0;
|
323
|
+
size_t offset = 0;
|
324
|
+
// `text` is going to be split into a list of nodes containing shorter segments
|
325
|
+
// of text, so we detach the memory buffer from text and use `cmark_chunk_dup` to
|
326
|
+
// create references to it. Later, `cmark_chunk_to_cstr` is used to convert
|
327
|
+
// the references into allocated buffers. The detached buffer is freed before we
|
328
|
+
// return.
|
329
|
+
cmark_chunk detached_chunk = text->as.literal;
|
330
|
+
text->as.literal = cmark_chunk_dup(&detached_chunk, 0, detached_chunk.len);
|
331
|
+
|
332
|
+
uint8_t *data = text->as.literal.data;
|
333
|
+
size_t remaining = text->as.literal.len;
|
334
|
+
|
335
|
+
while (true) {
|
336
|
+
size_t link_end;
|
337
|
+
uint8_t *at;
|
338
|
+
bool auto_mailto = true;
|
339
|
+
bool is_xmpp = false;
|
340
|
+
size_t rewind;
|
341
|
+
size_t max_rewind;
|
342
|
+
size_t np = 0;
|
343
|
+
|
344
|
+
if (offset >= remaining)
|
345
|
+
break;
|
292
346
|
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
size_t size = text->as.literal.len;
|
297
|
-
bool auto_mailto = true;
|
298
|
-
bool is_xmpp = false;
|
299
|
-
int rewind, max_rewind,
|
300
|
-
nb = 0, np = 0, ns = 0;
|
347
|
+
at = (uint8_t *)memchr(data + start + offset, '@', remaining - offset);
|
348
|
+
if (!at)
|
349
|
+
break;
|
301
350
|
|
302
|
-
|
303
|
-
return;
|
351
|
+
max_rewind = at - (data + start + offset);
|
304
352
|
|
305
|
-
|
306
|
-
|
353
|
+
found_at:
|
354
|
+
for (rewind = 0; rewind < max_rewind; ++rewind) {
|
355
|
+
uint8_t c = data[start + offset + max_rewind - rewind - 1];
|
307
356
|
|
308
|
-
|
309
|
-
|
310
|
-
return;
|
357
|
+
if (cmark_isalnum(c))
|
358
|
+
continue;
|
311
359
|
|
312
|
-
|
313
|
-
|
314
|
-
size -= max_rewind;
|
360
|
+
if (strchr(".+-_", c) != NULL)
|
361
|
+
continue;
|
315
362
|
|
316
|
-
|
317
|
-
|
363
|
+
if (strchr(":", c) != NULL) {
|
364
|
+
if (validate_protocol("mailto:", data + start + offset + max_rewind, rewind, max_rewind)) {
|
365
|
+
auto_mailto = false;
|
366
|
+
continue;
|
367
|
+
}
|
368
|
+
|
369
|
+
if (validate_protocol("xmpp:", data + start + offset + max_rewind, rewind, max_rewind)) {
|
370
|
+
auto_mailto = false;
|
371
|
+
is_xmpp = true;
|
372
|
+
continue;
|
373
|
+
}
|
374
|
+
}
|
318
375
|
|
319
|
-
|
320
|
-
|
376
|
+
break;
|
377
|
+
}
|
321
378
|
|
322
|
-
if (
|
379
|
+
if (rewind == 0) {
|
380
|
+
offset += max_rewind + 1;
|
323
381
|
continue;
|
382
|
+
}
|
383
|
+
|
384
|
+
assert(data[start + offset + max_rewind] == '@');
|
385
|
+
for (link_end = 1; link_end < remaining - offset - max_rewind; ++link_end) {
|
386
|
+
uint8_t c = data[start + offset + max_rewind + link_end];
|
324
387
|
|
325
|
-
|
326
|
-
if (validate_protocol("mailto:", data, rewind)) {
|
327
|
-
auto_mailto = false;
|
388
|
+
if (cmark_isalnum(c))
|
328
389
|
continue;
|
329
|
-
}
|
330
390
|
|
331
|
-
if (
|
332
|
-
|
333
|
-
|
391
|
+
if (c == '@') {
|
392
|
+
// Found another '@', so go back and try again with an updated offset and max_rewind.
|
393
|
+
offset += max_rewind + 1;
|
394
|
+
max_rewind = link_end - 1;
|
395
|
+
goto found_at;
|
396
|
+
} else if (c == '.' && link_end < remaining - offset - max_rewind - 1 &&
|
397
|
+
cmark_isalnum(data[start + offset + max_rewind + link_end + 1]))
|
398
|
+
np++;
|
399
|
+
else if (c == '/' && is_xmpp)
|
334
400
|
continue;
|
335
|
-
|
401
|
+
else if (c != '-' && c != '_')
|
402
|
+
break;
|
336
403
|
}
|
337
404
|
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
postprocess_text(parser, text, max_rewind + 1 + offset, depth + 1);
|
343
|
-
return;
|
344
|
-
}
|
345
|
-
|
346
|
-
for (link_end = 0; link_end < size; ++link_end) {
|
347
|
-
uint8_t c = data[link_end];
|
348
|
-
|
349
|
-
if (cmark_isalnum(c))
|
350
|
-
continue;
|
351
|
-
|
352
|
-
if (c == '@')
|
353
|
-
nb++;
|
354
|
-
else if (c == '.' && link_end < size - 1 && cmark_isalnum(data[link_end + 1]))
|
355
|
-
np++;
|
356
|
-
else if (c == '/' && is_xmpp)
|
405
|
+
if (link_end < 2 || np == 0 ||
|
406
|
+
(!cmark_isalpha(data[start + offset + max_rewind + link_end - 1]) &&
|
407
|
+
data[start + offset + max_rewind + link_end - 1] != '.')) {
|
408
|
+
offset += max_rewind + link_end;
|
357
409
|
continue;
|
358
|
-
|
359
|
-
break;
|
360
|
-
}
|
410
|
+
}
|
361
411
|
|
362
|
-
|
363
|
-
(!cmark_isalpha(data[link_end - 1]) && data[link_end - 1] != '.')) {
|
364
|
-
postprocess_text(parser, text, max_rewind + 1 + offset, depth + 1);
|
365
|
-
return;
|
366
|
-
}
|
412
|
+
link_end = autolink_delim(data + start + offset + max_rewind, link_end);
|
367
413
|
|
368
|
-
|
414
|
+
if (link_end == 0) {
|
415
|
+
offset += max_rewind + 1;
|
416
|
+
continue;
|
417
|
+
}
|
369
418
|
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
419
|
+
cmark_node *link_node = cmark_node_new_with_mem(CMARK_NODE_LINK, parser->mem);
|
420
|
+
cmark_strbuf buf;
|
421
|
+
cmark_strbuf_init(parser->mem, &buf, 10);
|
422
|
+
if (auto_mailto)
|
423
|
+
cmark_strbuf_puts(&buf, "mailto:");
|
424
|
+
cmark_strbuf_put(&buf, data + start + offset + max_rewind - rewind, (bufsize_t)(link_end + rewind));
|
425
|
+
link_node->as.link.url = cmark_chunk_buf_detach(&buf);
|
426
|
+
|
427
|
+
cmark_node *link_text = cmark_node_new_with_mem(CMARK_NODE_TEXT, parser->mem);
|
428
|
+
cmark_chunk email = cmark_chunk_dup(
|
429
|
+
&detached_chunk,
|
430
|
+
(bufsize_t)(start + offset + max_rewind - rewind),
|
431
|
+
(bufsize_t)(link_end + rewind));
|
432
|
+
cmark_chunk_to_cstr(parser->mem, &email);
|
433
|
+
link_text->as.literal = email;
|
434
|
+
cmark_node_append_child(link_node, link_text);
|
374
435
|
|
375
|
-
|
436
|
+
cmark_node_insert_after(text, link_node);
|
376
437
|
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
cmark_strbuf_puts(&buf, "mailto:");
|
382
|
-
cmark_strbuf_put(&buf, data - rewind, (bufsize_t)(link_end + rewind));
|
383
|
-
link_node->as.link.url = cmark_chunk_buf_detach(&buf);
|
384
|
-
|
385
|
-
cmark_node *link_text = cmark_node_new_with_mem(CMARK_NODE_TEXT, parser->mem);
|
386
|
-
cmark_chunk email = cmark_chunk_dup(
|
387
|
-
&text->as.literal,
|
388
|
-
offset + max_rewind - rewind,
|
389
|
-
(bufsize_t)(link_end + rewind));
|
390
|
-
cmark_chunk_to_cstr(parser->mem, &email);
|
391
|
-
link_text->as.literal = email;
|
392
|
-
cmark_node_append_child(link_node, link_text);
|
438
|
+
cmark_node *post = cmark_node_new_with_mem(CMARK_NODE_TEXT, parser->mem);
|
439
|
+
post->as.literal = cmark_chunk_dup(&detached_chunk,
|
440
|
+
(bufsize_t)(start + offset + max_rewind + link_end),
|
441
|
+
(bufsize_t)(remaining - offset - max_rewind - link_end));
|
393
442
|
|
394
|
-
|
443
|
+
cmark_node_insert_after(link_node, post);
|
395
444
|
|
396
|
-
|
397
|
-
|
398
|
-
(bufsize_t)(offset + max_rewind + link_end),
|
399
|
-
(bufsize_t)(size - link_end));
|
400
|
-
cmark_chunk_to_cstr(parser->mem, &post->as.literal);
|
445
|
+
text->as.literal = cmark_chunk_dup(&detached_chunk, (bufsize_t)start, (bufsize_t)(offset + max_rewind - rewind));
|
446
|
+
cmark_chunk_to_cstr(parser->mem, &text->as.literal);
|
401
447
|
|
402
|
-
|
448
|
+
text = post;
|
449
|
+
start += offset + max_rewind + link_end;
|
450
|
+
remaining -= offset + max_rewind + link_end;
|
451
|
+
offset = 0;
|
452
|
+
}
|
403
453
|
|
404
|
-
|
405
|
-
text->as.literal.
|
454
|
+
// Convert the reference to allocated memory.
|
455
|
+
assert(!text->as.literal.alloc);
|
456
|
+
cmark_chunk_to_cstr(parser->mem, &text->as.literal);
|
406
457
|
|
407
|
-
|
458
|
+
// Free the detached buffer.
|
459
|
+
cmark_chunk_free(parser->mem, &detached_chunk);
|
408
460
|
}
|
409
461
|
|
410
462
|
static cmark_node *postprocess(cmark_syntax_extension *ext, cmark_parser *parser, cmark_node *root) {
|
@@ -431,7 +483,7 @@ static cmark_node *postprocess(cmark_syntax_extension *ext, cmark_parser *parser
|
|
431
483
|
}
|
432
484
|
|
433
485
|
if (ev == CMARK_EVENT_ENTER && node->type == CMARK_NODE_TEXT) {
|
434
|
-
postprocess_text(parser, node
|
486
|
+
postprocess_text(parser, node);
|
435
487
|
}
|
436
488
|
}
|
437
489
|
|
data/ext/commonmarker/blocks.c
CHANGED
@@ -8,6 +8,7 @@
|
|
8
8
|
#include <stdlib.h>
|
9
9
|
#include <assert.h>
|
10
10
|
#include <stdio.h>
|
11
|
+
#include <limits.h>
|
11
12
|
|
12
13
|
#include "cmark_ctype.h"
|
13
14
|
#include "syntax_extension.h"
|
@@ -639,6 +640,14 @@ static cmark_node *finalize_document(cmark_parser *parser) {
|
|
639
640
|
}
|
640
641
|
|
641
642
|
finalize(parser, parser->root);
|
643
|
+
|
644
|
+
// Limit total size of extra content created from reference links to
|
645
|
+
// document size to avoid superlinear growth. Always allow 100KB.
|
646
|
+
if (parser->total_size > 100000)
|
647
|
+
parser->refmap->max_ref_size = parser->total_size;
|
648
|
+
else
|
649
|
+
parser->refmap->max_ref_size = 100000;
|
650
|
+
|
642
651
|
process_inlines(parser, parser->refmap, parser->options);
|
643
652
|
if (parser->options & CMARK_OPT_FOOTNOTES)
|
644
653
|
process_footnotes(parser);
|
@@ -698,6 +707,11 @@ static void S_parser_feed(cmark_parser *parser, const unsigned char *buffer,
|
|
698
707
|
const unsigned char *end = buffer + len;
|
699
708
|
static const uint8_t repl[] = {239, 191, 189};
|
700
709
|
|
710
|
+
if (len > UINT_MAX - parser->total_size)
|
711
|
+
parser->total_size = UINT_MAX;
|
712
|
+
else
|
713
|
+
parser->total_size += len;
|
714
|
+
|
701
715
|
if (parser->last_buffer_ended_with_cr && *buffer == '\n') {
|
702
716
|
// skip NL if last buffer ended with CR ; see #117
|
703
717
|
buffer++;
|
@@ -6,45 +6,45 @@ extern "C" {
|
|
6
6
|
#endif
|
7
7
|
|
8
8
|
#include "cmark-gfm-extension_api.h"
|
9
|
-
#include "cmark-
|
10
|
-
#include
|
9
|
+
#include "cmark-gfm_export.h"
|
10
|
+
#include <stdbool.h>
|
11
11
|
#include <stdint.h>
|
12
12
|
|
13
|
-
|
13
|
+
CMARK_GFM_EXPORT
|
14
14
|
void cmark_gfm_core_extensions_ensure_registered(void);
|
15
15
|
|
16
|
-
|
16
|
+
CMARK_GFM_EXPORT
|
17
17
|
uint16_t cmark_gfm_extensions_get_table_columns(cmark_node *node);
|
18
18
|
|
19
19
|
/** Sets the number of columns for the table, returning 1 on success and 0 on error.
|
20
20
|
*/
|
21
|
-
|
21
|
+
CMARK_GFM_EXPORT
|
22
22
|
int cmark_gfm_extensions_set_table_columns(cmark_node *node, uint16_t n_columns);
|
23
23
|
|
24
|
-
|
24
|
+
CMARK_GFM_EXPORT
|
25
25
|
uint8_t *cmark_gfm_extensions_get_table_alignments(cmark_node *node);
|
26
26
|
|
27
27
|
/** Sets the alignments for the table, returning 1 on success and 0 on error.
|
28
28
|
*/
|
29
|
-
|
29
|
+
CMARK_GFM_EXPORT
|
30
30
|
int cmark_gfm_extensions_set_table_alignments(cmark_node *node, uint16_t ncols, uint8_t *alignments);
|
31
31
|
|
32
|
-
|
32
|
+
CMARK_GFM_EXPORT
|
33
33
|
int cmark_gfm_extensions_get_table_row_is_header(cmark_node *node);
|
34
34
|
|
35
35
|
/** Sets whether the node is a table header row, returning 1 on success and 0 on error.
|
36
36
|
*/
|
37
|
-
|
37
|
+
CMARK_GFM_EXPORT
|
38
38
|
int cmark_gfm_extensions_set_table_row_is_header(cmark_node *node, int is_header);
|
39
39
|
|
40
|
-
|
40
|
+
CMARK_GFM_EXPORT
|
41
41
|
bool cmark_gfm_extensions_get_tasklist_item_checked(cmark_node *node);
|
42
42
|
/* For backwards compatibility */
|
43
43
|
#define cmark_gfm_extensions_tasklist_is_checked cmark_gfm_extensions_get_tasklist_item_checked
|
44
44
|
|
45
45
|
/** Sets whether a tasklist item is "checked" (completed), returning 1 on success and 0 on error.
|
46
46
|
*/
|
47
|
-
|
47
|
+
CMARK_GFM_EXPORT
|
48
48
|
int cmark_gfm_extensions_set_tasklist_item_checked(cmark_node *node, bool is_checked);
|
49
49
|
|
50
50
|
#ifdef __cplusplus
|
@@ -111,13 +111,13 @@ typedef struct cmark_mem {
|
|
111
111
|
* realloc and free.
|
112
112
|
*/
|
113
113
|
CMARK_GFM_EXPORT
|
114
|
-
cmark_mem *cmark_get_default_mem_allocator();
|
114
|
+
cmark_mem *cmark_get_default_mem_allocator(void);
|
115
115
|
|
116
116
|
/** An arena allocator; uses system calloc to allocate large
|
117
117
|
* slabs of memory. Memory in these slabs is not reused at all.
|
118
118
|
*/
|
119
119
|
CMARK_GFM_EXPORT
|
120
|
-
cmark_mem *cmark_get_arena_mem_allocator();
|
120
|
+
cmark_mem *cmark_get_arena_mem_allocator(void);
|
121
121
|
|
122
122
|
/** Resets the arena allocator, quickly returning all used memory
|
123
123
|
* to the operating system.
|
@@ -225,6 +225,11 @@ CMARK_GFM_EXPORT cmark_node *cmark_node_first_child(cmark_node *node);
|
|
225
225
|
*/
|
226
226
|
CMARK_GFM_EXPORT cmark_node *cmark_node_last_child(cmark_node *node);
|
227
227
|
|
228
|
+
/** Returns the footnote reference of 'node', or NULL if 'node' doesn't have a
|
229
|
+
* footnote reference.
|
230
|
+
*/
|
231
|
+
CMARK_GFM_EXPORT cmark_node *cmark_node_parent_footnote_def(cmark_node *node);
|
232
|
+
|
228
233
|
/**
|
229
234
|
* ## Iterator
|
230
235
|
*
|
data/ext/commonmarker/cmark.c
CHANGED
@@ -10,9 +10,9 @@
|
|
10
10
|
cmark_node_type CMARK_NODE_LAST_BLOCK = CMARK_NODE_FOOTNOTE_DEFINITION;
|
11
11
|
cmark_node_type CMARK_NODE_LAST_INLINE = CMARK_NODE_FOOTNOTE_REFERENCE;
|
12
12
|
|
13
|
-
int cmark_version() { return CMARK_GFM_VERSION; }
|
13
|
+
int cmark_version(void) { return CMARK_GFM_VERSION; }
|
14
14
|
|
15
|
-
const char *cmark_version_string() { return CMARK_GFM_VERSION_STRING; }
|
15
|
+
const char *cmark_version_string(void) { return CMARK_GFM_VERSION_STRING; }
|
16
16
|
|
17
17
|
static void *xcalloc(size_t nmem, size_t size) {
|
18
18
|
void *ptr = calloc(nmem, size);
|
@@ -38,7 +38,7 @@ static void xfree(void *ptr) {
|
|
38
38
|
|
39
39
|
cmark_mem CMARK_DEFAULT_MEM_ALLOCATOR = {xcalloc, xrealloc, xfree};
|
40
40
|
|
41
|
-
cmark_mem *cmark_get_default_mem_allocator() {
|
41
|
+
cmark_mem *cmark_get_default_mem_allocator(void) {
|
42
42
|
return &CMARK_DEFAULT_MEM_ALLOCATOR;
|
43
43
|
}
|
44
44
|
|