summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/blocks.c55
-rw-r--r--src/chunk.h9
-rw-r--r--src/inlines.c51
-rw-r--r--src/references.c36
4 files changed, 90 insertions, 61 deletions
diff --git a/src/blocks.c b/src/blocks.c
index 18fcdc4..8d6fd06 100644
--- a/src/blocks.c
+++ b/src/blocks.c
@@ -19,15 +19,15 @@ static node_block* make_block(int tag, int start_line, int start_column)
{
node_block* e;
- e = malloc(sizeof(node_block));
- memset(e, 0x0, sizeof(*e));
-
- e->tag = tag;
- e->open = true;
- e->start_line = start_line;
- e->start_column = start_column;
- e->end_line = start_line;
- strbuf_init(&e->string_content, 32);
+ e = calloc(1, sizeof(*e));
+ if(e != NULL) {
+ e->tag = tag;
+ e->open = true;
+ e->start_line = start_line;
+ e->start_column = start_column;
+ e->end_line = start_line;
+ strbuf_init(&e->string_content, 32);
+ }
return e;
}
@@ -310,14 +310,17 @@ static int parse_list_marker(chunk *input, int pos, struct ListData ** dataptr)
if (!isspace(peek_at(input, pos))) {
return 0;
}
- data = malloc(sizeof(struct ListData));
- data->marker_offset = 0; // will be adjusted later
- data->list_type = bullet;
- data->bullet_char = c;
- data->start = 1;
- data->delimiter = period;
- data->tight = false;
-
+ data = calloc(1, sizeof(*data));
+ if(data == NULL) {
+ return 0;
+ } else {
+ data->marker_offset = 0; // will be adjusted later
+ data->list_type = bullet;
+ data->bullet_char = c;
+ data->start = 1;
+ data->delimiter = period;
+ data->tight = false;
+ }
} else if (isdigit(c)) {
int start = 0;
@@ -332,13 +335,17 @@ static int parse_list_marker(chunk *input, int pos, struct ListData ** dataptr)
if (!isspace(peek_at(input, pos))) {
return 0;
}
- data = malloc(sizeof(struct ListData));
- data->marker_offset = 0; // will be adjusted later
- data->list_type = ordered;
- data->bullet_char = 0;
- data->start = start;
- data->delimiter = (c == '.' ? period : parens);
- data->tight = false;
+ data = calloc(1, sizeof(*data));
+ if(data == NULL) {
+ return 0;
+ } else {
+ data->marker_offset = 0; // will be adjusted later
+ data->list_type = ordered;
+ data->bullet_char = 0;
+ data->start = start;
+ data->delimiter = (c == '.' ? period : parens);
+ data->tight = false;
+ }
} else {
return 0;
}
diff --git a/src/chunk.h b/src/chunk.h
index f37a2f3..015bbf9 100644
--- a/src/chunk.h
+++ b/src/chunk.h
@@ -59,10 +59,11 @@ static inline unsigned char *chunk_to_cstr(chunk *c)
{
unsigned char *str;
- str = malloc(c->len + 1);
- memcpy(str, c->data, c->len);
- str[c->len] = 0;
-
+ str = calloc(c->len + 1, sizeof(*str));
+ if(str != NULL) {
+ memcpy(str, c->data, c->len);
+ str[c->len] = 0;
+ }
return str;
}
diff --git a/src/inlines.c b/src/inlines.c
index c95b46b..4744312 100644
--- a/src/inlines.c
+++ b/src/inlines.c
@@ -39,8 +39,10 @@ static unsigned char *bufdup(const unsigned char *buf)
if (buf) {
int len = strlen((char *)buf);
- new = malloc(len + 1);
- memcpy(new, buf, len + 1);
+ new = calloc(len + 1, sizeof(*new));
+ if(new != NULL) {
+ memcpy(new, buf, len + 1);
+ }
}
return new;
@@ -48,12 +50,14 @@ static unsigned char *bufdup(const unsigned char *buf)
static inline node_inl *make_link_(node_inl *label, unsigned char *url, unsigned char *title)
{
- node_inl* e = (node_inl*) malloc(sizeof(node_inl));
- e->tag = INL_LINK;
- e->content.linkable.label = label;
- e->content.linkable.url = url;
- e->content.linkable.title = title;
- e->next = NULL;
+ node_inl* e = calloc(1, sizeof(*e));
+ if(e != NULL) {
+ e->tag = INL_LINK;
+ e->content.linkable.label = label;
+ e->content.linkable.url = url;
+ e->content.linkable.title = title;
+ e->next = NULL;
+ }
return e;
}
@@ -75,29 +79,35 @@ inline static node_inl* make_link(node_inl* label, chunk url, chunk title)
inline static node_inl* make_inlines(int t, node_inl* contents)
{
- node_inl* e = (node_inl*) malloc(sizeof(node_inl));
- e->tag = t;
- e->content.inlines = contents;
- e->next = NULL;
+ node_inl * e = calloc(1, sizeof(*e));
+ if(e != NULL) {
+ e->tag = t;
+ e->content.inlines = contents;
+ e->next = NULL;
+ }
return e;
}
// Create an inline with a literal string value.
inline static node_inl* make_literal(int t, chunk s)
{
- node_inl* e = (node_inl*) malloc(sizeof(node_inl));
- e->tag = t;
- e->content.literal = s;
- e->next = NULL;
+ node_inl * e = calloc(1, sizeof(*e));
+ if(e != NULL) {
+ e->tag = t;
+ e->content.literal = s;
+ e->next = NULL;
+ }
return e;
}
// Create an inline with no value.
inline static node_inl* make_simple(int t)
{
- node_inl* e = (node_inl*) malloc(sizeof(node_inl));
- e->tag = t;
- e->next = NULL;
+ node_inl* e = calloc(1, sizeof(*e));
+ if(e != NULL) {
+ e->tag = t;
+ e->next = NULL;
+ }
return e;
}
@@ -382,6 +392,9 @@ cannotClose:
if (can_open)
{
istack = (inline_stack*)malloc(sizeof(inline_stack));
+ if (istack == NULL) {
+ return NULL;
+ }
istack->delim_count = numdelims;
istack->delim_char = c;
istack->first_inline = inl_text;
diff --git a/src/references.c b/src/references.c
index 31e2a3c..6759c2c 100644
--- a/src/references.c
+++ b/src/references.c
@@ -16,10 +16,12 @@ refhash(const unsigned char *link_ref)
static void reference_free(reference *ref)
{
- free(ref->label);
- free(ref->url);
- free(ref->title);
- free(ref);
+ if(ref != NULL) {
+ free(ref->label);
+ free(ref->url);
+ free(ref->title);
+ free(ref);
+ }
}
// normalize reference: collapse internal whitespace to single space,
@@ -31,6 +33,9 @@ static unsigned char *normalize_reference(chunk *ref)
strbuf normalized = GH_BUF_INIT;
unsigned char *result;
+ if(ref == NULL)
+ return NULL;
+
if (ref->len == 0)
return NULL;
@@ -75,14 +80,16 @@ extern void reference_create(reference_map *map, chunk *label, chunk *url, chunk
if (reflabel == NULL)
return;
- ref = malloc(sizeof(reference));
- ref->label = reflabel;
- ref->hash = refhash(ref->label);
- ref->url = clean_url(url);
- ref->title = clean_title(title);
- ref->next = NULL;
+ ref = calloc(1, sizeof(*ref));
+ if(ref != NULL) {
+ ref->label = reflabel;
+ ref->hash = refhash(ref->label);
+ ref->url = clean_url(url);
+ ref->title = clean_title(title);
+ ref->next = NULL;
- add_reference(map, ref);
+ add_reference(map, ref);
+ }
}
// Returns reference if refmap contains a reference with matching
@@ -118,6 +125,9 @@ void reference_map_free(reference_map *map)
{
unsigned int i;
+ if(map == NULL)
+ return;
+
for (i = 0; i < REFMAP_SIZE; ++i) {
reference *ref = map->table[i];
reference *next;
@@ -134,7 +144,5 @@ void reference_map_free(reference_map *map)
reference_map *reference_map_new(void)
{
- reference_map *map = malloc(sizeof(reference_map));
- memset(map, 0x0, sizeof(reference_map));
- return map;
+ return calloc(1, sizeof(reference_map));
}