Skip to content

Commit 40de14c

Browse files
Alearner12claude
andcommitted
node: address review — add BUFSIZE_MAX, drop realloc NULL check, document allocator contract
Per @nwellnhof feedback: - Remove realloc NULL check (allocators are assumed non-NULL by design). - Add BUFSIZE_MAX macro to buffer.h; use it in both node.c and buffer.c instead of hardcoded INT32_MAX. Per @jgm + @nwellnhof: - Document the allocator contract in cmark.h: allocation functions must not return NULL. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e0b65de commit 40de14c

4 files changed

Lines changed: 18 additions & 23 deletions

File tree

src/buffer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ void cmark_strbuf_grow(cmark_strbuf *buf, bufsize_t target_size) {
4040
if (target_size < buf->asize)
4141
return;
4242

43-
if (target_size > (bufsize_t)(INT32_MAX / 2)) {
43+
if (target_size > (bufsize_t)(BUFSIZE_MAX / 2)) {
4444
fprintf(stderr,
4545
"[cmark] cmark_strbuf_grow requests buffer with size > %d, aborting\n",
46-
(INT32_MAX / 2));
46+
(BUFSIZE_MAX / 2));
4747
abort();
4848
}
4949

src/buffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extern "C" {
1414
#endif
1515

1616
typedef int32_t bufsize_t;
17+
#define BUFSIZE_MAX INT32_MAX
1718

1819
typedef struct {
1920
cmark_mem *mem;

src/cmark.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ typedef struct cmark_iter cmark_iter;
9393
*/
9494

9595
/** Defines the memory allocation functions to be used by CMark
96-
* when parsing and allocating a document tree
96+
* when parsing and allocating a document tree. Allocation functions
97+
* must not return NULL; if they are unable to satisfy a request,
98+
* they must abort the program (e.g. by calling abort()).
9799
*/
98100
typedef struct cmark_mem {
99101
void *(*calloc)(size_t, size_t);

src/node.c

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <stdbool.h>
2-
#include <stdint.h>
32
#include <stdio.h>
43
#include <stdlib.h>
54
#include <string.h>
@@ -272,26 +271,19 @@ static bufsize_t cmark_set_cstr(cmark_mem *mem, unsigned char **dst,
272271
bufsize_t len;
273272

274273
if (src && src[0]) {
275-
size_t srclen = strlen(src);
276-
if (srclen > (size_t)(INT32_MAX - 1)) {
277-
fprintf(stderr,
278-
"[cmark] cmark_set_cstr: input exceeds %d bytes, aborting\n",
279-
INT32_MAX - 1);
280-
abort();
281-
}
282-
len = (bufsize_t)srclen;
283-
unsigned char *buf =
284-
(unsigned char *)mem->realloc(NULL, (size_t)len + 1);
285-
if (buf == NULL) {
286-
fprintf(stderr,
287-
"[cmark] cmark_set_cstr: allocator returned NULL, aborting\n");
288-
abort();
289-
}
290-
memcpy(buf, src, (size_t)len + 1);
291-
*dst = buf;
274+
size_t srclen = strlen(src);
275+
if (srclen > (size_t)BUFSIZE_MAX - 1) {
276+
fprintf(stderr,
277+
"[cmark] cmark_set_cstr: input exceeds %d bytes, aborting\n",
278+
BUFSIZE_MAX - 1);
279+
abort();
280+
}
281+
len = (bufsize_t)srclen;
282+
*dst = (unsigned char *)mem->realloc(NULL, (size_t)len + 1);
283+
memcpy(*dst, src, (size_t)len + 1);
292284
} else {
293-
len = 0;
294-
*dst = NULL;
285+
len = 0;
286+
*dst = NULL;
295287
}
296288
if (old) {
297289
mem->free(old);

0 commit comments

Comments
 (0)