Skip to content

Commit 6e4493d

Browse files
Fix GHSA-66g8-4hjf-77xh: don't run expense safety check which causes quadratic performance.
1 parent b44f479 commit 6e4493d

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

api_test/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,7 @@ int main() {
11331133
int retval;
11341134
test_batch_runner *runner = test_batch_runner_new();
11351135

1136+
cmark_enable_safety_checks(true);
11361137
version(runner);
11371138
constructor(runner);
11381139
accessors(runner);

src/node.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
#include "node.h"
66
#include "syntax_extension.h"
77

8+
/**
9+
* Expensive safety checks are off by default, but can be enabled
10+
* by calling cmark_enable_safety_checks().
11+
*/
12+
static bool enable_safety_checks = false;
13+
14+
void cmark_enable_safety_checks(bool enable) {
15+
enable_safety_checks = enable;
16+
}
17+
818
static void S_node_unlink(cmark_node *node);
919

1020
#define NODE_MEM(node) cmark_node_mem(node)
@@ -70,23 +80,23 @@ bool cmark_node_can_contain_type(cmark_node *node, cmark_node_type child_type) {
7080
}
7181

7282
static bool S_can_contain(cmark_node *node, cmark_node *child) {
73-
cmark_node *cur;
74-
7583
if (node == NULL || child == NULL) {
7684
return false;
7785
}
7886
if (NODE_MEM(node) != NODE_MEM(child)) {
7987
return 0;
8088
}
8189

82-
// Verify that child is not an ancestor of node or equal to node.
83-
cur = node;
84-
do {
85-
if (cur == child) {
86-
return false;
87-
}
88-
cur = cur->parent;
89-
} while (cur != NULL);
90+
if (enable_safety_checks) {
91+
// Verify that child is not an ancestor of node or equal to node.
92+
cmark_node *cur = node;
93+
do {
94+
if (cur == child) {
95+
return false;
96+
}
97+
cur = cur->parent;
98+
} while (cur != NULL);
99+
}
90100

91101
return cmark_node_can_contain_type(node, (cmark_node_type) child->type);
92102
}

src/node.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ static CMARK_INLINE bool CMARK_NODE_INLINE_P(cmark_node *node) {
145145

146146
CMARK_GFM_EXPORT bool cmark_node_can_contain_type(cmark_node *node, cmark_node_type child_type);
147147

148+
/**
149+
* Enable (or disable) extra safety checks. These extra checks cause
150+
* extra performance overhead (in some cases quadratic), so they are only
151+
* intended to be used during testing.
152+
*/
153+
CMARK_GFM_EXPORT void cmark_enable_safety_checks(bool enable);
154+
148155
#ifdef __cplusplus
149156
}
150157
#endif

0 commit comments

Comments
 (0)