Skip to content

Commit 39ae2a8

Browse files
fixes existing data races
rdar://75568341
1 parent 482d443 commit 39ae2a8

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

extensions/core-extensions.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <pthread.h>
2+
13
#include "cmark-gfm-core-extensions.h"
24
#include "autolink.h"
35
#include "strikethrough.h"
@@ -7,21 +9,28 @@
79
#include "registry.h"
810
#include "plugin.h"
911

12+
pthread_mutex_t extensions_lock;
13+
1014
static int core_extensions_registration(cmark_plugin *plugin) {
15+
pthread_mutex_lock(&extensions_lock);
1116
cmark_plugin_register_syntax_extension(plugin, create_table_extension());
1217
cmark_plugin_register_syntax_extension(plugin,
1318
create_strikethrough_extension());
1419
cmark_plugin_register_syntax_extension(plugin, create_autolink_extension());
1520
cmark_plugin_register_syntax_extension(plugin, create_tagfilter_extension());
1621
cmark_plugin_register_syntax_extension(plugin, create_tasklist_extension());
22+
pthread_mutex_unlock(&extensions_lock);
1723
return 1;
1824
}
1925

20-
void cmark_gfm_core_extensions_ensure_registered(void) {
21-
static int registered = 0;
26+
pthread_mutex_t registered_lock;
27+
static _Atomic int registered = 0;
2228

29+
void cmark_gfm_core_extensions_ensure_registered(void) {
30+
pthread_mutex_lock(&registered_lock);
2331
if (!registered) {
2432
cmark_register_plugin(core_extensions_registration);
2533
registered = 1;
2634
}
35+
pthread_mutex_unlock(&registered_lock);
2736
}

src/inlines.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdlib.h>
22
#include <string.h>
33
#include <stdio.h>
4+
#include <pthread.h>
45

56
#include "cmark_ctype.h"
67
#include "cmark-gfm_config.h"
@@ -64,7 +65,7 @@ typedef struct subject{
6465
} subject;
6566

6667
// Extensions may populate this.
67-
static int8_t SKIP_CHARS[256];
68+
static _Atomic int8_t SKIP_CHARS[256];
6869

6970
static CMARK_INLINE bool S_is_line_end_char(char c) {
7071
return (c == '\n' || c == '\r');
@@ -1321,7 +1322,7 @@ static cmark_node *handle_newline(subject *subj) {
13211322
}
13221323

13231324
// "\r\n\\`&_*[]<!"
1324-
static int8_t SPECIAL_CHARS[256] = {
1325+
static _Atomic int8_t SPECIAL_CHARS[256] = {
13251326
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13261327
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
13271328
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -1349,6 +1350,8 @@ static char SMART_PUNCT_CHARS[] = {
13491350
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13501351
};
13511352

1353+
pthread_mutex_t chars_lock;
1354+
13521355
static bufsize_t subject_find_special_char(subject *subj, int options) {
13531356
bufsize_t n = subj->pos + 1;
13541357

@@ -1364,15 +1367,19 @@ static bufsize_t subject_find_special_char(subject *subj, int options) {
13641367
}
13651368

13661369
void cmark_inlines_add_special_character(unsigned char c, bool emphasis) {
1370+
pthread_mutex_lock(&chars_lock);
13671371
SPECIAL_CHARS[c] = 1;
13681372
if (emphasis)
13691373
SKIP_CHARS[c] = 1;
1374+
pthread_mutex_unlock(&chars_lock);
13701375
}
13711376

13721377
void cmark_inlines_remove_special_character(unsigned char c, bool emphasis) {
1378+
pthread_mutex_lock(&chars_lock);
13731379
SPECIAL_CHARS[c] = 0;
13741380
if (emphasis)
13751381
SKIP_CHARS[c] = 0;
1382+
pthread_mutex_unlock(&chars_lock);
13761383
}
13771384

13781385
static cmark_node *try_extensions(cmark_parser *parser,

0 commit comments

Comments
 (0)