Skip to content

Commit baf45d4

Browse files
committed
updated to cmark 0.26.1
1 parent 3026c7a commit baf45d4

27 files changed

+1813
-1733
lines changed

blocks.c

+268-258
Large diffs are not rendered by default.

buffer.c

+20-57
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
#include <stdio.h>
66
#include <stdlib.h>
77
#include <stdint.h>
8+
#include <limits.h>
89

910
#include "config.h"
1011
#include "cmark_ctype.h"
1112
#include "buffer.h"
13+
#include "memory.h"
1214

1315
/* Used as default value for cmark_strbuf->ptr so that people can always
1416
* assume ptr is non-NULL and zero terminated even for new cmark_strbufs.
@@ -19,7 +21,9 @@ unsigned char cmark_strbuf__initbuf[1];
1921
#define MIN(x, y) ((x < y) ? x : y)
2022
#endif
2123

22-
void cmark_strbuf_init(cmark_strbuf *buf, bufsize_t initial_size) {
24+
void cmark_strbuf_init(cmark_mem *mem, cmark_strbuf *buf,
25+
bufsize_t initial_size) {
26+
buf->mem = mem;
2327
buf->asize = 0;
2428
buf->size = 0;
2529
buf->ptr = cmark_strbuf__initbuf;
@@ -28,68 +32,28 @@ void cmark_strbuf_init(cmark_strbuf *buf, bufsize_t initial_size) {
2832
cmark_strbuf_grow(buf, initial_size);
2933
}
3034

31-
void cmark_strbuf_overflow_err() {
32-
fprintf(stderr, "String buffer overflow");
33-
abort();
34-
}
35-
36-
static CMARK_INLINE void S_strbuf_grow_by(cmark_strbuf *buf, size_t add) {
37-
size_t target_size = (size_t)buf->size + add;
38-
39-
if (target_size < add /* Integer overflow. */
40-
|| target_size > BUFSIZE_MAX /* Truncation overflow. */
41-
) {
42-
cmark_strbuf_overflow_err();
43-
return; /* unreachable */
44-
}
45-
46-
if ((bufsize_t)target_size >= buf->asize)
47-
cmark_strbuf_grow(buf, (bufsize_t)target_size);
35+
static CMARK_INLINE void S_strbuf_grow_by(cmark_strbuf *buf, bufsize_t add) {
36+
cmark_strbuf_grow(buf, buf->size + add);
4837
}
4938

5039
void cmark_strbuf_grow(cmark_strbuf *buf, bufsize_t target_size) {
51-
unsigned char *new_ptr;
40+
assert(target_size > 0);
5241

5342
if (target_size < buf->asize)
5443
return;
5544

56-
if (buf->asize == 0) {
57-
new_ptr = NULL;
58-
} else {
59-
new_ptr = buf->ptr;
60-
}
45+
if (target_size > (bufsize_t)(INT32_MAX / 2))
46+
abort();
6147

6248
/* Oversize the buffer by 50% to guarantee amortized linear time
6349
* complexity on append operations. */
64-
size_t new_size = (size_t)target_size + (size_t)target_size / 2;
65-
66-
/* Account for terminating null byte. */
50+
bufsize_t new_size = target_size + target_size / 2;
6751
new_size += 1;
68-
69-
/* round allocation up to multiple of 8 */
7052
new_size = (new_size + 7) & ~7;
7153

72-
if (new_size < (size_t)target_size /* Integer overflow. */
73-
|| new_size > BUFSIZE_MAX /* Truncation overflow. */
74-
) {
75-
if (target_size >= BUFSIZE_MAX) {
76-
/* No space for terminating null byte. */
77-
cmark_strbuf_overflow_err();
78-
return; /* unreachable */
79-
}
80-
/* Oversize by the maximum possible amount. */
81-
new_size = BUFSIZE_MAX;
82-
}
83-
84-
new_ptr = (unsigned char *)realloc(new_ptr, new_size);
85-
86-
if (!new_ptr) {
87-
perror("realloc in cmark_strbuf_grow");
88-
abort();
89-
}
90-
91-
buf->asize = (bufsize_t)new_size;
92-
buf->ptr = new_ptr;
54+
buf->ptr = (unsigned char *)buf->mem->realloc(buf->asize ? buf->ptr : NULL,
55+
new_size);
56+
buf->asize = new_size;
9357
}
9458

9559
bufsize_t cmark_strbuf_len(const cmark_strbuf *buf) { return buf->size; }
@@ -99,9 +63,9 @@ void cmark_strbuf_free(cmark_strbuf *buf) {
9963
return;
10064

10165
if (buf->ptr != cmark_strbuf__initbuf)
102-
free(buf->ptr);
66+
buf->mem->free(buf->ptr);
10367

104-
cmark_strbuf_init(buf, 0);
68+
cmark_strbuf_init(buf->mem, buf, 0);
10569
}
10670

10771
void cmark_strbuf_clear(cmark_strbuf *buf) {
@@ -128,7 +92,7 @@ void cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data,
12892

12993
void cmark_strbuf_sets(cmark_strbuf *buf, const char *string) {
13094
cmark_strbuf_set(buf, (const unsigned char *)string,
131-
string ? cmark_strbuf_safe_strlen(string) : 0);
95+
string ? strlen(string) : 0);
13296
}
13397

13498
void cmark_strbuf_putc(cmark_strbuf *buf, int c) {
@@ -149,8 +113,7 @@ void cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data,
149113
}
150114

151115
void cmark_strbuf_puts(cmark_strbuf *buf, const char *string) {
152-
cmark_strbuf_put(buf, (const unsigned char *)string,
153-
cmark_strbuf_safe_strlen(string));
116+
cmark_strbuf_put(buf, (const unsigned char *)string, strlen(string));
154117
}
155118

156119
void cmark_strbuf_copy_cstr(char *data, bufsize_t datasize,
@@ -184,10 +147,10 @@ unsigned char *cmark_strbuf_detach(cmark_strbuf *buf) {
184147

185148
if (buf->asize == 0) {
186149
/* return an empty string */
187-
return (unsigned char *)calloc(1, 1);
150+
return (unsigned char *)buf->mem->calloc(1, 1);
188151
}
189152

190-
cmark_strbuf_init(buf, 0);
153+
cmark_strbuf_init(buf->mem, buf, 0);
191154
return data;
192155
}
193156

buffer.h

+9-20
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,35 @@
55
#include <stdarg.h>
66
#include <string.h>
77
#include <limits.h>
8+
#include <stdint.h>
89
#include "config.h"
10+
#include "cmark.h"
911

1012
#ifdef __cplusplus
1113
extern "C" {
1214
#endif
1315

14-
typedef int bufsize_t;
16+
typedef int32_t bufsize_t;
1517

1618
typedef struct {
19+
cmark_mem *mem;
1720
unsigned char *ptr;
1821
bufsize_t asize, size;
1922
} cmark_strbuf;
2023

2124
extern unsigned char cmark_strbuf__initbuf[];
2225

23-
#define GH_BUF_INIT \
24-
{ cmark_strbuf__initbuf, 0, 0 }
25-
#define BUFSIZE_MAX INT_MAX
26+
#define CMARK_BUF_INIT(mem) \
27+
{ mem, cmark_strbuf__initbuf, 0, 0 }
2628

2729
/**
2830
* Initialize a cmark_strbuf structure.
2931
*
30-
* For the cases where GH_BUF_INIT cannot be used to do static
32+
* For the cases where CMARK_BUF_INIT cannot be used to do static
3133
* initialization.
3234
*/
33-
void cmark_strbuf_init(cmark_strbuf *buf, bufsize_t initial_size);
35+
void cmark_strbuf_init(cmark_mem *mem, cmark_strbuf *buf,
36+
bufsize_t initial_size);
3437

3538
/**
3639
* Grow the buffer to hold at least `target_size` bytes.
@@ -72,20 +75,6 @@ void cmark_strbuf_trim(cmark_strbuf *buf);
7275
void cmark_strbuf_normalize_whitespace(cmark_strbuf *s);
7376
void cmark_strbuf_unescape(cmark_strbuf *s);
7477

75-
/* Print error and abort. */
76-
void cmark_strbuf_overflow_err(void);
77-
78-
static CMARK_INLINE bufsize_t cmark_strbuf_check_bufsize(size_t size) {
79-
if (size > BUFSIZE_MAX) {
80-
cmark_strbuf_overflow_err();
81-
}
82-
return (bufsize_t)size;
83-
}
84-
85-
static CMARK_INLINE bufsize_t cmark_strbuf_safe_strlen(const char *str) {
86-
return cmark_strbuf_check_bufsize(strlen(str));
87-
}
88-
8978
#ifdef __cplusplus
9079
}
9180
#endif

chunk.h

+24-21
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#include <string.h>
55
#include <stdlib.h>
66
#include <assert.h>
7-
#include "cmark_ctype.h"
7+
#include "cmark.h"
88
#include "buffer.h"
9+
#include "memory.h"
10+
#include "cmark_ctype.h"
911

1012
#define CMARK_CHUNK_EMPTY \
1113
{ NULL, 0, 0 }
@@ -16,9 +18,9 @@ typedef struct {
1618
bufsize_t alloc; // also implies a NULL-terminated string
1719
} cmark_chunk;
1820

19-
static CMARK_INLINE void cmark_chunk_free(cmark_chunk *c) {
21+
static CMARK_INLINE void cmark_chunk_free(cmark_mem *mem, cmark_chunk *c) {
2022
if (c->alloc)
21-
free(c->data);
23+
mem->free(c->data);
2224

2325
c->data = NULL;
2426
c->alloc = 0;
@@ -48,56 +50,57 @@ static CMARK_INLINE void cmark_chunk_trim(cmark_chunk *c) {
4850
cmark_chunk_rtrim(c);
4951
}
5052

51-
static CMARK_INLINE bufsize_t cmark_chunk_strchr(cmark_chunk *ch, int c,
52-
bufsize_t offset) {
53+
static CMARK_INLINE bufsize_t
54+
cmark_chunk_strchr(cmark_chunk *ch, int c, bufsize_t offset) {
5355
const unsigned char *p =
5456
(unsigned char *)memchr(ch->data + offset, c, ch->len - offset);
5557
return p ? (bufsize_t)(p - ch->data) : ch->len;
5658
}
5759

58-
static CMARK_INLINE const char *cmark_chunk_to_cstr(cmark_chunk *c) {
60+
static CMARK_INLINE const char *cmark_chunk_to_cstr(cmark_mem *mem,
61+
cmark_chunk *c) {
5962
unsigned char *str;
6063

6164
if (c->alloc) {
6265
return (char *)c->data;
6366
}
64-
str = (unsigned char *)malloc(c->len + 1);
65-
if (str != NULL) {
66-
if (c->len > 0) {
67-
memcpy(str, c->data, c->len);
68-
}
69-
str[c->len] = 0;
67+
str = (unsigned char *)mem->calloc(c->len + 1, 1);
68+
if (c->len > 0) {
69+
memcpy(str, c->data, c->len);
7070
}
71+
str[c->len] = 0;
7172
c->data = str;
7273
c->alloc = 1;
7374

7475
return (char *)str;
7576
}
7677

77-
static CMARK_INLINE void cmark_chunk_set_cstr(cmark_chunk *c, const char *str) {
78-
if (c->alloc) {
79-
free(c->data);
80-
}
78+
static CMARK_INLINE void cmark_chunk_set_cstr(cmark_mem *mem, cmark_chunk *c,
79+
const char *str) {
80+
unsigned char *old = c->alloc ? c->data : NULL;
8181
if (str == NULL) {
8282
c->len = 0;
8383
c->data = NULL;
8484
c->alloc = 0;
8585
} else {
86-
c->len = cmark_strbuf_safe_strlen(str);
87-
c->data = (unsigned char *)malloc(c->len + 1);
86+
c->len = (bufsize_t)strlen(str);
87+
c->data = (unsigned char *)mem->calloc(c->len + 1, 1);
8888
c->alloc = 1;
8989
memcpy(c->data, str, c->len + 1);
9090
}
91+
if (old != NULL) {
92+
mem->free(old);
93+
}
9194
}
9295

9396
static CMARK_INLINE cmark_chunk cmark_chunk_literal(const char *data) {
94-
bufsize_t len = data ? cmark_strbuf_safe_strlen(data) : 0;
97+
bufsize_t len = data ? (bufsize_t)strlen(data) : 0;
9598
cmark_chunk c = {(unsigned char *)data, len, 0};
9699
return c;
97100
}
98101

99-
static CMARK_INLINE cmark_chunk cmark_chunk_dup(const cmark_chunk *ch,
100-
bufsize_t pos, bufsize_t len) {
102+
static CMARK_INLINE cmark_chunk
103+
cmark_chunk_dup(const cmark_chunk *ch, bufsize_t pos, bufsize_t len) {
101104
cmark_chunk c = {ch->data + pos, len, 0};
102105
return c;
103106
}

cmark.c

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ int cmark_version() { return CMARK_VERSION; }
1010

1111
const char *cmark_version_string() { return CMARK_VERSION_STRING; }
1212

13+
static void *xcalloc(size_t nmem, size_t size) {
14+
void *ptr = calloc(nmem, size);
15+
if (!ptr)
16+
abort();
17+
return ptr;
18+
}
19+
20+
static void *xrealloc(void *ptr, size_t size) {
21+
void *new_ptr = realloc(ptr, size);
22+
if (!new_ptr)
23+
abort();
24+
return new_ptr;
25+
}
26+
27+
cmark_mem DEFAULT_MEM_ALLOCATOR = {xcalloc, xrealloc, free};
28+
1329
char *cmark_markdown_to_html(const char *text, size_t len, int options) {
1430
cmark_node *doc;
1531
char *result;

0 commit comments

Comments
 (0)