5
5
#include <stdio.h>
6
6
#include <stdlib.h>
7
7
#include <stdint.h>
8
+ #include <limits.h>
8
9
9
10
#include "config.h"
10
11
#include "cmark_ctype.h"
11
12
#include "buffer.h"
13
+ #include "memory.h"
12
14
13
15
/* Used as default value for cmark_strbuf->ptr so that people can always
14
16
* assume ptr is non-NULL and zero terminated even for new cmark_strbufs.
@@ -19,7 +21,9 @@ unsigned char cmark_strbuf__initbuf[1];
19
21
#define MIN (x , y ) ((x < y) ? x : y)
20
22
#endif
21
23
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 ;
23
27
buf -> asize = 0 ;
24
28
buf -> size = 0 ;
25
29
buf -> ptr = cmark_strbuf__initbuf ;
@@ -28,68 +32,28 @@ void cmark_strbuf_init(cmark_strbuf *buf, bufsize_t initial_size) {
28
32
cmark_strbuf_grow (buf , initial_size );
29
33
}
30
34
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 );
48
37
}
49
38
50
39
void cmark_strbuf_grow (cmark_strbuf * buf , bufsize_t target_size ) {
51
- unsigned char * new_ptr ;
40
+ assert ( target_size > 0 ) ;
52
41
53
42
if (target_size < buf -> asize )
54
43
return ;
55
44
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 ();
61
47
62
48
/* Oversize the buffer by 50% to guarantee amortized linear time
63
49
* 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 ;
67
51
new_size += 1 ;
68
-
69
- /* round allocation up to multiple of 8 */
70
52
new_size = (new_size + 7 ) & ~7 ;
71
53
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 ;
93
57
}
94
58
95
59
bufsize_t cmark_strbuf_len (const cmark_strbuf * buf ) { return buf -> size ; }
@@ -99,9 +63,9 @@ void cmark_strbuf_free(cmark_strbuf *buf) {
99
63
return ;
100
64
101
65
if (buf -> ptr != cmark_strbuf__initbuf )
102
- free (buf -> ptr );
66
+ buf -> mem -> free (buf -> ptr );
103
67
104
- cmark_strbuf_init (buf , 0 );
68
+ cmark_strbuf_init (buf -> mem , buf , 0 );
105
69
}
106
70
107
71
void cmark_strbuf_clear (cmark_strbuf * buf ) {
@@ -128,7 +92,7 @@ void cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data,
128
92
129
93
void cmark_strbuf_sets (cmark_strbuf * buf , const char * string ) {
130
94
cmark_strbuf_set (buf , (const unsigned char * )string ,
131
- string ? cmark_strbuf_safe_strlen (string ) : 0 );
95
+ string ? strlen (string ) : 0 );
132
96
}
133
97
134
98
void cmark_strbuf_putc (cmark_strbuf * buf , int c ) {
@@ -149,8 +113,7 @@ void cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data,
149
113
}
150
114
151
115
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 ));
154
117
}
155
118
156
119
void cmark_strbuf_copy_cstr (char * data , bufsize_t datasize ,
@@ -184,10 +147,10 @@ unsigned char *cmark_strbuf_detach(cmark_strbuf *buf) {
184
147
185
148
if (buf -> asize == 0 ) {
186
149
/* return an empty string */
187
- return (unsigned char * )calloc (1 , 1 );
150
+ return (unsigned char * )buf -> mem -> calloc (1 , 1 );
188
151
}
189
152
190
- cmark_strbuf_init (buf , 0 );
153
+ cmark_strbuf_init (buf -> mem , buf , 0 );
191
154
return data ;
192
155
}
193
156
0 commit comments