diff options
Diffstat (limited to 'src/chunk.h')
-rw-r--r-- | src/chunk.h | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/src/chunk.h b/src/chunk.h index 54c4b16..4bb4980 100644 --- a/src/chunk.h +++ b/src/chunk.h @@ -7,6 +7,8 @@ #include "cmark_ctype.h" #include "buffer.h" +#define CMARK_CHUNK_EMPTY { NULL, 0, 0 } + typedef struct { unsigned char *data; int len; @@ -64,7 +66,9 @@ static inline const char *cmark_chunk_to_cstr(cmark_chunk *c) } str = (unsigned char *)malloc(c->len + 1); if(str != NULL) { - memcpy(str, c->data, c->len); + if(c->len > 0){ + memcpy(str, c->data, c->len); + } str[c->len] = 0; } c->data = str; @@ -78,10 +82,17 @@ static inline void cmark_chunk_set_cstr(cmark_chunk *c, const char *str) if (c->alloc) { free(c->data); } - c->len = strlen(str); - c->data = (unsigned char *)malloc(c->len + 1); - c->alloc = 1; - memcpy(c->data, str, c->len + 1); + if (str == NULL) { + c->len = 0; + c->data = NULL; + c->alloc = 0; + } + else { + c->len = strlen(str); + c->data = (unsigned char *)malloc(c->len + 1); + c->alloc = 1; + memcpy(c->data, str, c->len + 1); + } } static inline cmark_chunk cmark_chunk_literal(const char *data) |