File tree Expand file tree Collapse file tree 4 files changed +37
-0
lines changed Expand file tree Collapse file tree 4 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -523,6 +523,7 @@ static void process_footnotes(cmark_parser *parser) {
523
523
}
524
524
}
525
525
526
+ cmark_unlink_footnotes_map (map );
526
527
cmark_map_free (map );
527
528
}
528
529
Original file line number Diff line number Diff line change @@ -38,3 +38,26 @@ void cmark_footnote_create(cmark_map *map, cmark_node *node) {
38
38
cmark_map * cmark_footnote_map_new (cmark_mem * mem ) {
39
39
return cmark_map_new (mem , footnote_free );
40
40
}
41
+
42
+ // Before calling `cmark_map_free` on a map with `cmark_footnotes`, first
43
+ // unlink all of the footnote nodes before freeing their memory.
44
+ //
45
+ // Sometimes, two (unused) footnote nodes can end up referencing each other,
46
+ // which as they get freed up by calling `cmark_map_free` -> `footnote_free` ->
47
+ // etc, can lead to a use-after-free error.
48
+ //
49
+ // Better to `unlink` every footnote node first, setting their next, prev, and
50
+ // parent pointers to NULL, and only then walk thru & free them up.
51
+ void cmark_unlink_footnotes_map (cmark_map * map ) {
52
+ cmark_map_entry * ref ;
53
+ cmark_map_entry * next ;
54
+
55
+ ref = map -> refs ;
56
+ while (ref ) {
57
+ next = ref -> next ;
58
+ if (((cmark_footnote * )ref )-> node ) {
59
+ cmark_node_unlink (((cmark_footnote * )ref )-> node );
60
+ }
61
+ ref = next ;
62
+ }
63
+ }
Original file line number Diff line number Diff line change @@ -18,6 +18,8 @@ typedef struct cmark_footnote cmark_footnote;
18
18
void cmark_footnote_create (cmark_map * map , cmark_node * node );
19
19
cmark_map * cmark_footnote_map_new (cmark_mem * mem );
20
20
21
+ void cmark_unlink_footnotes_map (cmark_map * map );
22
+
21
23
#ifdef __cplusplus
22
24
}
23
25
#endif
Original file line number Diff line number Diff line change @@ -354,3 +354,14 @@ Footnotes interacting with strikethrough should not lead to a use-after-free pt2
354
354
.
355
355
<p>[^~~is~~1]</p>
356
356
````````````````````````````````
357
+
358
+ Adjacent unused footnotes definitions should not lead to a use after free
359
+
360
+ ```````````````````````````````` example footnotes autolink strikethrough table
361
+ Hello world
362
+
363
+
364
+ [^a]:[^b]:
365
+ .
366
+ <p>Hello world</p>
367
+ ````````````````````````````````
You can’t perform that action at this time.
0 commit comments